0

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

1
  • can you provide the definition of CustomObject please? Commented Nov 18, 2011 at 15:33

1 Answer 1

2

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));
    }

}
Sign up to request clarification or add additional context in comments.

7 Comments

Don't i have to add something in the xaml to tell it where to look for Client ?
You just need to bind to the property. The binding will be updated automatically when the value changes.
It doesn't work this is why i ask. Shouldn't i add something like StaticResource or something else i the xaml ?
The thing is that i'm not really sure how window.xaml can find B ?
@djfoxmccloud, just set B as the DataContext of the Window; I assumed it was already the case...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.