Lycus
Lycus

Reputation: 430

How to "chain" bindings?

I have a Entity Framework model bound to an object like this:

public class Object
{
    public string _name; //actual name
    public string _display_name;
    ....
}

The display_name, for the most part, will be the same as the actual_name. So, in a WPF window, I have a view with two inputs (TextBox).

The first input is bound to a View Model property DisplayName. The purpose of this is so that the second input (the display name) is, by default, equal to the first (the actual name):

<TextBox x:Name="NameInputBox" Margin="5,5,5,5" Width="100" MaxLength="50"
                         Text="{Binding Path=Name, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}" />

The second input is using MultiBinding. The first binding in the MultiBinding is to the first input's Text property. This properly updates the second box to be equal to the first box.

The second binding is bound to the DisplayName property in the view model. The DisplayName property is NOT being set, despite the fact that the second input text is changing:

<TextBox x:Name="DisplayNameInput" Margin="5,5,5,5" Width="100" MaxLength="50">
    <TextBox.Text>
        <MultiBinding Converter="{StaticResource TwoToOneConverter}">
            <Binding ElementName="NameInputBox" Path="Text" Mode="OneWay" UpdateSourceTrigger="PropertyChanged" />
            <Binding Path="DisplayName" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" />
        </MultiBinding>
    </TextBox.Text>
</TextBox>

Here are the properties:

public String Name
{
    get { return _name; }
    set
    {
        _name = value;
        OnPropertyChanged("Name");
    }
}

public string DisplayName
{
    get { return _display_name; }
    set
    {
        _display_name = value;
        OnPropertyChanged("DisplayName");
    }
}

Finally, my converter:

/// <summary>
/// When given two inputs, just take one of them.
/// </summary>
public object Convert(object[] values, Type targetType, object parameter, 
    System.Globalization.CultureInfo culture) 
{
    return values[0];
}

/// <summary>
/// Return the value, unchanged, as two values.
/// </summary>
public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
    System.Globalization.CultureInfo culture)
{
    return new object[] { value, value };
}

Anyone have any ideas as to what I'm doing incorrectly? I feel like it has something to do with the converter, since I'm not using it in the way depicted in the online samples. Is there another way to achieve what I'm attempting?

Upvotes: 0

Views: 364

Answers (1)

svick
svick

Reputation: 244837

I think it would make more sense to do this from code: If _display_name is null, DisplayName returns Name. This would also mean you would need to add OnPropertyChanged("DisplayName"); to your Name setter:

public String Name
{
    get { return _name; }
    set
    {
        _name = value;
        OnPropertyChanged("Name");
        OnPropertyChanged("DisplayName");
    }
}

public string DisplayName
{
    get { return _display_name ?? _name; }
    set
    {
        _display_name = value;
        OnPropertyChanged("DisplayName");
    }
}

With this, you could use normal Binding instead of MultiBinding, so there should be no problem with setting the value.

Upvotes: 1

Related Questions