Struct
Struct

Reputation: 970

Change Binding depending on RadioButton

I have a textbox with an Binding to an ObservableCollection with a triple tuple and want to change the Binding depending on two radiobuttons:

XAML:

<ItemsControl ItemsSource="{Binding DataInformation, Mode=OneWay}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
                <TextBox x:Name="xTextbox" Grid.Row="0"
                        IsReadOnly="True"
                        Text="{Binding Path=Item2 , Mode=OneWay}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl  >

<RadioButton Grid.Row="1"
        x:Name="PatoRadioButton" 
        Width="150"
        Content="Type Pato"
        Checked="PatoRadioButtonChecked" />

<RadioButton Grid.Row="2"
        x:Name="FifoRadioButton" 
        Width="150"
        Content="Type Fifo"
        Checked="FifoRadioButtonChecked" />

VIEWMODEL:

private ObservableCollection<Tuple<int, string, string>> dataInformation;

public ObservableCollection<Tuple<int, string, string>> DataInformation
{
    get
    {
        return DataInformation;
    }

    set
    {
        dataInformation = value;
        NotifyPropertyChanged("DataInformation");
    }
}

QUESTION:

How can I change the Text="{Binding Path=Item2 , Mode=OneWay}" of the TextBox depending on the checked RadioButton?

Upvotes: 1

Views: 892

Answers (1)

Flat Eric
Flat Eric

Reputation: 8111

You could add 2 Triggers to the TextBox of your DataTemplate:

<DataTemplate>
    <TextBox x:Name="xTextbox" Grid.Row="0" IsReadOnly="True">
        <TextBox.Style>
            <Style TargetType="TextBox">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsChecked, 
                                 ElementName=PatoRadioButton}" Value="True">
                        <DataTrigger.Setters>
                            <Setter Property="Text" Value="{Binding Path=Item2, 
                                    Mode=OneWay" />
                        </DataTrigger.Setters>
                    </DataTrigger>

                    <DataTrigger Binding="{Binding IsChecked, 
                                 ElementName=FifoRadioButton}" Value="True">
                        <DataTrigger.Setters>
                            <Setter Property="Text" Value="{Binding Path=Item3, 
                                    Mode=OneWay}" />
                        </DataTrigger.Setters>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>
</DataTemplate>

The triggers will set the Binding of the TextBlock when the IsChecked property is changed.

Upvotes: 3

Related Questions