WPF textblock binding in XAML

前端 未结 5 2027
萌比男神i
萌比男神i 2021-01-22 21:40

I\'m updating some existing WPF code and my application has a number of textblocks defined like this:



        
相关标签:
5条回答
  • 2021-01-22 22:25

    Try this:

    In code:

    public MyBusinessObject Instance { get; set; }
    
    Instance = new MyBusinessObject();
    

    In XAML:

    <TextBlock Text="{Binding Instance.PropertyNameHere" />
    
    0 讨论(0)
  • 2021-01-22 22:26

    Try to instantiate ObjectA in the same way as you are doing for PropertyA (Ie. as a property, with a public getter / setter, and calling OnPropertyChanged), then your XAML can be :

    <TextBlock Text="{Binding ObjectA.PropertyNameHere}" />
    
    0 讨论(0)
  • 2021-01-22 22:34

    Before <Run Text="{Binding ObjectA.PropertyNameHere}" /> will work you have to make ObjectA itself a property because binding will only work with properties not fields.

    // my business object also contains another object like this
    public SomeOtherObject ObjectA { get; set; }
    
    public MyBusinessObject()
    {
        // constructor
        ObjectA = new SomeOtherObject();
    }
    
    0 讨论(0)
  • 2021-01-22 22:35

    You can simply type this:

    <TextBlock Text="{Binding ObjectA.PropertyNameHere"/>
    

    You may want to implement INotifyPropertyChanged within your ObjectA class, as changing properties of the class will not be picked up by the PropertyChanged methods in your MyBusinessObject class.

    0 讨论(0)
  • 2021-01-22 22:42

    You can do a same as you do for PropertyA like follows,

    OnPropertyChanged(new PropertyChangedEventArgs("ObjectA"));
    

    on Designer XAML,

    <TextBlock x:Name="ObjectAProperty" Text="{Binding ObjectA.PropertyNameHere}" />
    
    0 讨论(0)
提交回复
热议问题