我正在尝试创建一个UserControl,它是一个图形项目的图例, 我已经定义了它,因此它有一个带有图形名称的标签,其中一个复选框 定义我们是否显示它与否以及带有图形颜色的矩形。自定义属性的用户控件
XAML中这样定义:
<UserControl x:Class="Resources.LegendItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d">
<UserControl.Template>
<ControlTemplate>
<StackPanel Margin="1,0,0,0" Orientation="Horizontal">
<CheckBox Name="cb" IsChecked="{TemplateBinding IsGraphVisible}" >
<StackPanel Margin="1,0,0,0" Orientation="Horizontal">
<Rectangle Name="rec" RadiusX="2" RadiusY="2" Height="10" Width="10" />
<Label Name="lab" Content="{TemplateBinding GraphName}" />
</StackPanel>
</CheckBox>
</StackPanel>
</ControlTemplate>
</UserControl.Template>
</UserControl>
和CS文件是:
namespace Resources
{
public partial class LegendItem : UserControl
{
public static readonly DependencyProperty IsGraphVisibleProperty = DependencyProperty.Register("IsGraphVisible", typeof(Boolean), typeof(LegendItem));
public static readonly DependencyProperty GraphNameProperty = DependencyProperty.Register("GraphName", typeof(String), typeof(LegendItem));
public bool IsGraphVisible
{
get { return (bool)GetValue(IsGraphVisibleProperty); }
set { SetValue(IsGraphVisibleProperty, value); }
}
public string GraphName
{
get { return (string)GetValue(GraphNameProperty); }
set { SetValue(GraphNameProperty, value); }
}
public LegendItem()
{
InitializeComponent();
}
}
}
但是,当我编译它,我得到一个错误“无法找到静态成员‘IsGraphVisibleProperty’在类型'控制'上。“ 任何帮助,将不胜感激。
顺便说一句,为什么是它的控制,而不是用户控件模板? – EvAlex 2012-04-17 08:08:09
因为控制是可以模板化的最一般的控制。 Template属性首先出现在Control中。 – 2012-04-17 08:10:50
UserControls不允许设置模板。 – GazTheDestroyer 2012-04-17 08:26:35