23

最も基本的な例でさえ、これを機能させようとして、私は気が狂ってしまいます。私は一生、仕事に拘束されることはできません。これは、私にとってうまくいかない非常に簡単な例です。私は何か間違ったことをしているに違いありません。

コントロール ライブラリ アセンブリのカスタム コントロール:

public class TestControl : Control
{
    public static readonly DependencyProperty TestPropProperty =
        DependencyProperty.Register("TestProp", typeof(string), typeof(TestControl), new UIPropertyMetadata(null));

    public string TestProp
    {
        get { return (string)GetValue(TestPropProperty); }
        set { SetValue(TestPropProperty, value); }
    }

    static TestControl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(TestControl), new FrameworkPropertyMetadata(typeof(TestControl)));
    }
}

そしてその XAML テンプレート:

<Style TargetType="{x:Type local:TestControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:TestControl}">
                <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                    <StackPanel>
                        <TextBlock Text="Testing..." />
                        <Label Content="{Binding TestProp}" Padding="10" />
                    </StackPanel>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

コントロール ライブラリへの参照を使用して、wpf ウィンドウでコントロールを使用する XAML を次に示します。

<Grid>
    <ItemsControl Name="mylist">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <my:TestControl TestProp="{Binding Path=Name}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

コードビハインドは次のとおりです。

public partial class Test2 : Window
{
    public class TestObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string PropertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
        }

        private int _id;
        public int id
        {
            get { return _id; }
            set { _id = value; OnPropertyChanged("id"); }
        }

        private string _Name;
        public string Name
        {
            get { return _Name; }
            set { _Name = value; OnPropertyChanged("Name"); }
        }
    }

    public Test2()
    {
        InitializeComponent();

        mylist.ItemsSource = new TestObject[]
        {
            new TestObject(){ id = 1, Name = "Tedd" },
            new TestObject(){ id = 2, Name = "Fred" },
            new TestObject(){ id = 3, Name = "Jim" },
            new TestObject(){ id = 4, Name = "Jack" },
        };
    }
}

この例を実行すると、コントロールの 4 つのインスタンスが得られますが、それぞれに "Testing..." TextBlock しか表示されません。私のラベルは決して拘束されません。私は何を誤解していて、間違っていますか?

4

1 に答える 1

25

適切なバインディングソースを設定していません。どちらかをRelativeSourceに設定する必要があります:

<Label Content="{Binding TestProp, RelativeSource={RelativeSource Mode=TemplatedParent}}" />

または、TemplateBindingを使用します。

<Label Content="{TemplateBinding TestProp}"/>
于 2012-08-10T06:45:56.977 に答える