所以我有這個自定義控件庫組成的兩個控件。出於某種原因,我不斷收到錯誤,如「無法綁定ContentPresenter的屬性,因爲沒有名稱爲'Content'的屬性,類型爲...」,並且「成員'模板'無法識別或無法訪問。」我所有的代碼似乎都符合要求,甚至在搜索谷歌(一百次以上)之後,我一直無法弄清楚什麼能阻止我的解決方案構建。WPF C# - 成員「模板」無法識別或無法訪問?
浪費了一個小時,試圖找出這一點,我什麼都沒有。下面的代碼:
Generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Imagin.Controls">
<Style TargetType="{x:Type local:WorkSpace}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:WorkSpace}">
<ContentPresenter ContentSource="Content" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type local:Tile}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:Tile}">
<ContentPresenter ContentSource="Content" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Tile.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Imagin.Controls
{
public class Tile : Border
{
public string Title { get; set; }
public bool IsClosable { get; set; }
public bool IsLockable { get; set; }
public Tiles TileType { get; set; }
public Orientations Orientation { get; set; }
static Tile()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(Tile), new FrameworkPropertyMetadata(typeof(Tile)));
}
}
}
WorkSpace.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Imagin.Controls
{
public enum Orientations
{
Top,
TopMiddle,
Middle,
BottomMiddle,
Bottom,
Left,
LeftMiddle,
RightMiddle,
Right
}
public enum Tiles
{
Window,
Tabbed,
Column
}
public class WorkSpace : Grid
{
static WorkSpace()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(WorkSpace), new FrameworkPropertyMetadata(typeof(WorkSpace)));
}
}
}
有什麼建議?
我不知道如何工作區從電網甚至繼承有一個模板屬性? 它不從控制類繼承.. 我會證明這個使用混合,但很遺憾,我沒有在這臺機器上混合的工作副本。也許明天.. 進一步說,這是一些控制。 也許它沒有Content屬性,這就是錯誤引用的內容。 – 2014-10-11 00:03:21
基本上,我試圖讓WorkSpace成爲一個網格狀的控件,可以嵌套子控件(「Tiles」,它具有類似邊框的功能),並使用模板屬性來重構要顯示的元素。在我的主要項目中,我已經對相同的這些對象進行了重新模板化,沒有出現問題。或者這種事情是不允許的?當談到創建自定義控件時,我仍然是一個初學者。這實際上是我嘗試的第一次。 – 2014-10-11 00:09:09
網格不是一個控件它是一個FrameworkElement。所有控件都是FrameworkElement,但不是所有的FrameworkElement都是控件,只有控件可以模板化。 – 2014-10-11 00:12:48