2012-04-17 167 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