【问题标题】:Custom Control Dependency Property Binding自定义控件依赖属性绑定
【发布时间】:2012-08-07 10:59:25
【问题描述】:

我要疯了,即使是最基本的例子也能正常工作。我这辈子都无法工作。这是一个对我不起作用的超级简单示例。我一定是做错了什么。

我的控件库程序集中的自定义控件:

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" },
        };
    }
}

运行此示例为我提供了四个控件实例,但是我只在每个实例上看到“正在测试...”TextBlock。我的标签从不绑定。我有什么误会和做错了什么?

【问题讨论】:

    标签: wpf wpf-controls dependency-properties


    【解决方案1】:

    您没有设置正确的绑定源。你要么必须设置RelativeSource:

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

    或者使用TemplateBinding:

    <Label Content="{TemplateBinding TestProp}"/>
    

    【讨论】:

      猜你喜欢
      • 2012-08-29
      • 2019-05-26
      • 1970-01-01
      • 1970-01-01
      • 2013-10-16
      • 2011-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多