用户控件具有自定义属性

时间:2012-04-17 08:02:27

标签: c# wpf xaml user-controls wpf-controls

我正在尝试创建一个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();
        }

    }
}

但是当我编译它时,我得到一个错误“无法在类型'Control'上找到静态成员'IsGraphVisibleProperty'。” 任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

根本不需要模板。 UserControl允许直接声明XAML。您无法在UserControl

中设置模板
<UserControl x:Class="Resources.LegendItem" x:Name="MyControl"
         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">

        <StackPanel Margin="1,0,0,0" Orientation="Horizontal">
            <CheckBox Name="cb" IsChecked="{Binding ElementName=MyControl, Path=IsGraphVisible}" >
                <StackPanel Margin="1,0,0,0" Orientation="Horizontal">
                    <Rectangle Name="rec" RadiusX="2" RadiusY="2" Height="10" Width="10" />
                    <Label Name="lab" Content="{Binding ElementName=MyControl, Path=GraphName}" />
                </StackPanel>
            </CheckBox>
        </StackPanel>
</UserControl>

答案 1 :(得分:1)

您需要在TargetType="{x:Type Resources.LegendItem}"上指定ControlTemplate,否则默认为Control的模板,您就会收到错误。