具有自定义属性的用户控件

本文关键字:用户 控件 自定义属性 | 更新日期: 2023-09-27 18:31:17

我正在尝试创建一个用户控件,它是一个图形项的图例,我已经定义了它,所以它有一个带有图形名称的标签,一个复选框定义我们是否显示它以及带有图形颜色的矩形。

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'。任何帮助将不胜感激。

具有自定义属性的用户控件

你根本不需要模板。 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>
您需要

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