首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > CAD教程 >

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

2012-09-16 
【WPF】自定义类作为UserControl的依赖属性的binding问题!!!定义一个类:C# code public class Student : Dep

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

C# code
 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:
C# code
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:
XML code
<StackPanel>        <TextBox  x:Name="tb1" Text="demo"/>              <TextBlock x:Name="tb2" Text="demo"/>    </StackPanel>


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

主程序对应代码:
C# code
  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成功? 这是为什么呢?应该怎么做!

[解决办法]
问题解决?
[解决办法]
LZ,神马意思,代码看不明白呢

热点排行