2012-04-17 168 views
2

我正在尝试创建一个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’在类型'控制'上。“ 任何帮助,将不胜感激。

回答

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

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

+0

顺便说一句,为什么是它的控制,而不是用户控件模板? – EvAlex 2012-04-17 08:08:09

+0

因为控制是可以模板化的最一般的控制。 Template属性首先出现在Control中。 – 2012-04-17 08:10:50

+0

UserControls不允许设置模板。 – GazTheDestroyer 2012-04-17 08:26:35