简体   繁体   中英

Binding dynamic objects wpf

I am trying to bind a custom object that can be dynamicaly changed to a displaed element.

My window.xaml has this for now :

<StackPanel Height="310" HorizontalAlignment="Left" Margin="12,12,0,0" Name="Configuration_stackPanel" VerticalAlignment="Top" Width="264" Grid.Column="1">
<Label Content="{Binding Path=Client}" Height="22" HorizontalAlignment="Left" Margin="20,0,0,0" Name="Client" VerticalAlignment="Top" />
</StackPanel>

In window.xaml.cs, i've got member which is

public CustomObject B;

A CustomObject has a client member. B.Client, gets the client name (which is a string) among other things

What should i do to display B.Client and have it change when it is change in the code.

ie : if in the code i do B.Client="foo" then foo is displayed and if i do B.Client="bar", bar is displayed instead of foo.

Thanks in advance
F

Your CustomObject class must implement the INotifyPropertyChanged interface:

public class CustomObject : INotifyPropertyChanged
{

    private string _client;
    public string Client
    {
        get { return _client; }
        set
        {
            _client = value;
            OnPropertyChanged("Client");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
        handler(this, new PropertyChangedEventArgs(propertyName));
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM