feng8080 2016-08-30 09:07 采纳率: 0%
浏览 6207

WPF 自定义控件依赖属性绑定

自定义一个简单控件,添加依赖属性,想在xmal中绑定设置依赖属性的值,绑定不起作用求问是怎么回事?

 public partial class LabelEx : UserControl
    {
        public LabelEx()
        {
            InitializeComponent();
            Data = "000";
            this.DataContext = this;
        }


        public static readonly DependencyProperty DataProperty = DependencyProperty.Register(
                    "Data", typeof(string), typeof(LabelEx), new FrameworkPropertyMetadata(""));

        public string Data
        {
            get { return (string)GetValue(DataProperty);}
            set {  SetValue(DataProperty, value);}
        }


        public string Title
        {
            get { return title.Text; }
            set { title.Text = value; }
        }
    }

如果将控件属性绑定到Data上能正常工作,如下代码:

 <src:LabelEx Data="{Binding Text, ElementName=txt}"></src:LabelEx>

但是我想绑定到对象属性上就不能正常工作,绑定不起作用,前台xmal代码:

 <src:LabelEx Data="{Binding XName}"></src:LabelEx>

后台CS代码

```public partial class Window1 : Window, INotifyPropertyChanged
{
private float? age;

    public float? Age
    {
        get { return age; }
        set { age = value; NotifyPropertyChanged("Age"); }
    }

    private float? score;

    public float? Score
    {
        get { return score; }
        set { score = value; NotifyPropertyChanged("Score"); }
    }

    private string xname;
    public string XName
    {
        get { return xname; }
        set { xname = value; NotifyPropertyChanged("XName"); }
    }

    public Window1()
    {
        InitializeComponent();
        this.DataContext = this;
        xname = "1234";
        age = 60;
        score = 380;
    }







  • 写回答

2条回答 默认 最新

  • dabocaiqq 2017-02-23 19:01
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?