我有一个带有三元组的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}}?
RadioButton
Text="{Binding Path=Item2 , Mode=OneWay}"
答案 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}}。