How do I Bind 2 controls to 1 field AND also access the 2 control values for ConvertBack?

482 views Asked by At

In a DataGridTemplateColumn DataTemplate, I want to bind 2 controls to a string field of format "[name]:[value]" i.e. the string is delimited by colon ":". I need to bind control a) to the [name] part and control b) the value part.

I have been able to successfully use an IValueConverter to split the string for display:

public class NameAndValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string rtn = ""; 

        string[] split = value.ToString().Split(':');

        if (split.Count() == 2)
        {
            if(parameter.ToString() == "Name")
                rtn = split[0];

            if(parameter.ToString() == "Value")
                rtn = split[1];
        }

        return rtn;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new InvalidOperationException("NameAndValueConverter can only be used OneWay.");
    }
}

And the XAML:

<DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal">
            <StackPanel.Resources>
                <local:NameAndValueConverter x:Key="NameAndValueConverter" />
            </StackPanel.Resources>
            <TextBox x:Name="namePart" Text="{Binding Path=FieldType, Converter={StaticResource NameAndValueConverter}, ConverterParameter='Name'}" />
            <TextBox x:Name="valuePart" Text="{Binding Path=FieldType, Converter={StaticResource NameAndValueConverter}, ConverterParameter='Value'}" />
        </StackPanel>
    </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>

But the data may be edited in the Textboxes so how can I access the 2 TextBox values in ConvertBack so that they can be joined again?

1

There are 1 answers

0
Damascus On

Doing this in the XAML:

<DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal">
            <StackPanel.Resources>
                <local:NameAndValueConverter x:Key="NameAndValueConverter" />
            </StackPanel.Resources>
            <TextBox x:Name="namePart" Text="{Binding Path=FieldType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource NameAndValueConverter}, ConverterParameter='Name'}" />
            <TextBox x:Name="valuePart" Text="{Binding Path=FieldType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource NameAndValueConverter}, ConverterParameter='Value'}" />
        </StackPanel>
    </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>

should be enough. You need a TwoWay binding to edit from UI, and when you set UpdateSourceTrigger to PropertyChanged the TextBox will update itself automatically when the property will be modified in the ViewModel (you'd obviously need to implement INotifyPropertyChanged )