【WPF】自定义类作为UserControl的依赖属性的binding问题!!!

-小仙- 2012-08-13 10:02:42
定义一个类:

public class Student : DependencyObject
{
public string Name
{
get { return (string)GetValue(NameProperty); }
set { SetValue(NameProperty, value); }
}

// Using a DependencyProperty as the backing store for Name. This enables animation, styling, binding, etc...
public static readonly DependencyProperty NameProperty =
DependencyProperty.Register("Name", typeof(string), typeof(Student));
}


定义一个UserControl:

public partial class UsercontrolBinding : UserControl
{
public UsercontrolBinding()
{
InitializeComponent();

stu = new Student();
stu.Name = "ABC";

tb1.SetBinding(TextBox.TextProperty, new Binding("Name") { Source = stu });
tb2.SetBinding(TextBlock.TextProperty, new Binding("TecName") { Source = this });
}

public Student stu
{
get { return (Student)GetValue(stuProperty); }
set { SetValue(stuProperty, value); }
}

// Using a DependencyProperty as the backing store for stu. This enables animation, styling, binding, etc...
public static readonly DependencyProperty stuProperty =
DependencyProperty.Register("stu", typeof(Student), typeof(UsercontrolBinding));




public string TecName
{
get { return (string)GetValue(TecNameProperty); }
set { SetValue(TecNameProperty, value); }
}

// Using a DependencyProperty as the backing store for TecName. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TecNameProperty =
DependencyProperty.Register("TecName", typeof(string), typeof(UsercontrolBinding));



}

界面XAML:

<StackPanel>
<TextBox x:Name="tb1" Text="demo"/>
<TextBlock x:Name="tb2" Text="demo"/>
</StackPanel>


然后在主程序中使用:
<l:UsercontrolBinding x:Name="ub1" />

主程序对应代码:


public MainWindow()
{
InitializeComponent();
stuB = new Student()
{
Name="Zhang",
};

TecName = "TecYang";
ub1.SetBinding(UsercontrolBinding.TecNameProperty, new Binding("TecName") { Source = this });
ub1.SetBinding(UsercontrolBinding.stuProperty, new Binding("stuB") { Source = this });
}
public string TecName
{
get { return (string)GetValue(TecNameProperty); }
set { SetValue(TecNameProperty, value); }
}

// Using a DependencyProperty as the backing store for TecName. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TecNameProperty =
DependencyProperty.Register("TecName", typeof(string), typeof(MainWindow));

public Student stuB
{
get { return (Student)GetValue(stuBProperty); }
set { SetValue(stuBProperty, value); }
}

// Using a DependencyProperty as the backing store for stuB. This enables animation, styling, binding, etc...
public static readonly DependencyProperty stuBProperty =
DependencyProperty.Register("stuB", typeof(Student), typeof(MainWindow));





TecName 是string类型,可以binding成功,而Student 类型的stuB却不可以binding成功? 这是为什么呢?应该怎么做!
...全文
759 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
hanq1314 2013-05-02
  • 打赏
  • 举报
回复
属性为int 或者string 都好使 一旦附加对象类型的属性 binding 就不能用了
Bullatus 2012-08-13
  • 打赏
  • 举报
回复
问题解决?
-小仙- 2012-08-13
  • 打赏
  • 举报
回复
原来是binding出错了:
应该是

public UsercontrolBinding()
{
InitializeComponent();

stu = new Student();
stu.Name = "ABC";

tb1.SetBinding(TextBox.TextProperty, new Binding("stu.Name") { Source = this});
tb2.SetBinding(TextBlock.TextProperty, new Binding("TecName") { Source = this });
}


binding的源必须是对象,而stu在UsercontrolBinding 定以为依赖属性的,它不是一个对象
mmmm433 2012-08-13
  • 打赏
  • 举报
回复
LZ,神马意思,代码看不明白呢

8,737

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧