SRM
SRM

Reputation: 1377

In WPF is it possible to bind to a property of a property in my DataContext?

I need to bind a TextBox's Text to a property of a class that itself a property of my DataContext. So, for example if I have a class called ServerInstance and it has a string property called Name. I expose a ServerInstance as a property called SelectedInstance in my code behind (viewmodel in this case). My question is how can I bind a TextBox to the Name property of the SelectedInstance property?

Here is my ViewModel/Code behind:

public class ViewModel : Notifier
{
    private MobileServerInstance m_instance = null;
    private RelayCommand m_addCommand = null;
    private RelayCommand m_clearCommand = null;

    public MobileServerInstance ServerInstance
    {
        get { return m_instance; }
        set { m_instance = value; OnPropertyChanged("ServerInstance"); }
    }

    public ICommand AddCommand
    {
        get
        {
            if (m_addCommand == null)
            {
                m_addCommand = new RelayCommand(parameter=>Add(parameter), parameter=>CanAdd(parameter));
            }

            return m_addCommand;
        }
    }

    public ICommand ClearCommand
    {
        get
        {
            if (m_clearCommand == null)
            {
                m_clearCommand = new RelayCommand(parameter => Clear(parameter), parameter => CanClear(parameter));
            }

            return m_clearCommand;
        }
    }

    private bool CanClear(object parameter)
    {
        return m_instance != null;
    }

    private void Clear(object parameter)
    {
        m_instance = null;
    }

    private bool CanAdd(object parameter)
    {
        return m_instance == null;
    }

    private void Add(object parameter)
    {
        m_instance = new MobileServerInstance();
    }
}

Notifier is a base class that implements the INotifyPropertyChanged interface and provides a protected OnPropertyChanged method which raises the PropertyChange event - typical pattern there.

Here is my simple class that I use for the DataContext:

public class MobileServerInstance : Notifier
{
    private string m_name = "Name";
    private string m_alias = "Alias";

    public string Name
    {
        get { return m_name; }
        set { m_name = value; OnPropertyChanged("Name"); }
    }

    public string Alias
    {
        get { return m_alias; }
        set { m_alias = value; OnPropertyChanged("Alias"); }
    }
}

And finally my xaml (this is a POC so the UI is very simple):

<Window x:Class="WpfApplication4.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src="clr-namespace:WpfApplication4"
    Title="Window1" Height="300" Width="500">

    <Window.DataContext>
        <src:ViewModel />
    </Window.DataContext>

    <StackPanel>
        <StackPanel Orientation="Horizontal">
            <Label Content="Name:" Margin="0 0 2 0" />
            <ComboBox Margin="0 0 8 0" Width="125" />
            <Button Margin="0 0 4 0" Content="Add" Command="{Binding AddCommand}" Width="75" />
            <Button Content="Clear" Command="{Binding ClearCommand}" Width="75" />
        </StackPanel>

        <StackPanel Margin="20 5 20 5"">
            <StackPanel Orientation="Horizontal">
                <Label Width="40" Content="Name:" Margin="0 0 2 0" />
                <TextBox Width="250" Text="{Binding ServerInstance.Name}" />
            </StackPanel>
            <StackPanel Orientation="Horizontal" Margin="0 5 0 0">
                <Label Width="40" Content="Alias:" Margin="0 0 2 0" />
                <TextBox Width="250" Text="{Binding ServerInstance.Alias}" />
            </StackPanel>
        </StackPanel>
    </StackPanel>
</Window>

Thanks, in advance, for any help you can provide.

Upvotes: 0

Views: 729

Answers (1)

Felice Pollano
Felice Pollano

Reputation: 33252

{Binding Path=ServerInstance.Name} must work, of course to see Name changing value even ServerInstance should be an INotifyPropertyChanged.

Upvotes: 5

Related Questions