WPF: Data binding with code

前端 未结 2 1484
孤街浪徒
孤街浪徒 2020-12-16 20:23

How do I use data-binding from code (C# or VB)?

This is what I have so far, but it is displaying Binding.ToString instead of m_Rep.FirstName

2条回答
  •  自闭症患者
    2020-12-16 20:50

    You should use m_Rep as a Source of Binding

    I have some sample C# code for you as below

    Person myDataSource = new Person("Joe");  
    // Name is a property which you want to bind  
    Binding myBinding = new Binding("Name");  
    myBinding.Source = myDataSource;  
    // myText is an instance of TextBlock  
    myText.SetBinding(TextBlock.TextProperty, myBinding);  
    

    Hope to help

提交回复
热议问题