【发布时间】:2014-10-10 23:41:53
【问题描述】:
所以我有这个由两个控件组成的自定义控件库。出于某种原因,我不断收到错误消息,例如“无法绑定 ContentPresenter 的属性,因为类型上没有名为‘Content’的属性……”和“成员‘模板’无法识别或不可访问”。我所有的代码似乎都是一致的,即使在搜索谷歌(一百次)之后,我也无法弄清楚是什么阻止了我的解决方案构建。
浪费了一个小时试图弄清楚这一点,但我仍然一无所获。代码如下:
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)));
}
}
}
有什么建议吗?
【问题讨论】:
-
我不明白从 Grid 继承的 Workspace 甚至还有 Template 属性?它不是从 Control 类继承的。我会使用 Blend 来证明这一点,但遗憾的是我在这台机器上没有 blend 的工作副本。也许明天.. 更进一步说这是一些控制。也许它没有 Content 属性,这就是错误所指的。
-
基本上,我正在尝试使 WorkSpace 成为一个类似网格的控件,可以嵌套子控件(“Tiles”,具有类似边框的功能),并且正在使用模板属性来重组哪些元素将被显示。在我的主要项目中,我已经毫无问题地重新设计了相同的此类对象。或者这种事情是不允许的?在创建自定义控件时,我仍然是一个初学者。这实际上是我第一次尝试。
-
Grid 不是 Control 它是 FrameworkElement。所有控件都是 FrameworkElements 但并非所有 FrameworkElement 都是 Controls ,只有 Controls 可以模板化。
标签: c# wpf windows xaml templates