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();
        }

    }
}

しかし、コンパイルすると、「タイプ「Control」で静的メンバー「IsGraphVisibleProperty」が見つかりません」というエラーが発生します。どんな助けでもいただければ幸いです。

4

2 に答える 2

2

テンプレートはまったく必要ありません。UserControlXAML を直接宣言できます。でテンプレートを設定することはできません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>
于 2012-04-17T08:18:33.927 に答える
1

で指定する必要がありTargetType="{x:Type Resources.LegendItem}"ますControlTemplate。そうしないと、デフォルトでのテンプレートにControlなり、そのエラーが発生します。

于 2012-04-17T08:06:45.900 に答える