根据RadioButton更改绑定

时间:2014-12-10 16:02:26

标签: c# wpf binding radio-button tuples

我有一个带有三元组的ObservableCollection绑定的文本框,并希望根据两个单选按钮更改Binding:

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" />

视图模型:

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

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

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

问题:

如何根据选中的Text="{Binding Path=Item2 , Mode=OneWay}"更改TextBox的{​​{1}}?

  • 然后检查PatoRadioButton:RadioButton
  • 然后检查FifoRadioButton:Text="{Binding Path=Item2 , Mode=OneWay}"

1 个答案:

答案 0 :(得分:3)

您可以向Triggers的{​​{1}}添加2 TextBox

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> 属性发生变化时,触发器将设置Binding的{​​{1}}。