加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

wpf – 自定义控件依赖属性绑定

发布时间:2020-12-14 00:49:07 所属栏目:百科 来源:网络整理
导读:我会疯狂地试图让这个工作与最基本的例子.我无法为我的生活带来约束力.这是一个超级容易的例子,不适合我.我必须做错事. 我的自定义控件在我的控制库程序集中: public class TestControl : Control{ public static readonly DependencyProperty TestPropProp
我会疯狂地试图让这个工作与最基本的例子.我无法为我的生活带来约束力.这是一个超级容易的例子,不适合我.我必须做错事.

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

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.我的标签永远不会被束缚.我有什么误会和做错了?

您尚未设置正确的绑定源.您将要设置 RelativeSource:
<Label Content="{Binding TestProp,RelativeSource={RelativeSource Mode=TemplatedParent}}" />

或使用TemplateBinding:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读