如何以声明方式从 ComboBox 的内容中设置 RadioButton 的 IsChecked 属性?
我有一个组合框,其中包含以下项目:a1、a2、a3、a4,我有两个单选按钮 r1 和 r2。 这就是我想要实现的目标: 如果用户从组合框中选择项目 a2,则 r1 的 IsChecked 属性应设置为 true。如果用户从组合框中选择项目 a3 或 a4,则 r2 的 IsChecked 属性应设置为 true。我想以声明方式完成此任务;即不使用转换器。 这是我的代码并提前致谢:
<Window x:Class="BMSystem.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<Style x:Key="myRadioActivator1">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Content, ElementName=comboBox1}" Value="R2">
<Setter Property="RadioButton.IsChecked" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
<Style x:Key="myRadioActivator2">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Content, ElementName=comboBox1}" Value="R3">
<Setter Property="RadioButton.IsChecked" Value="True"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Content, ElementName=comboBox1}" Value="R4">
<Setter Property="RadioButton.IsChecked" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<ComboBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" SelectionChanged="comboBox1_SelectionChanged">
<ComboBoxItem>R1</ComboBoxItem>
<ComboBoxItem>R2</ComboBoxItem>
<ComboBoxItem>R3</ComboBoxItem>
<ComboBoxItem>R4</ComboBoxItem>
</ComboBox>
<RadioButton Height="16" HorizontalAlignment="Left" Margin="10,43,0,0" Name="r1" VerticalAlignment="Top" Width="120" Style="{StaticResource myRadioActivator1}">
</RadioButton>
<RadioButton Height="16" HorizontalAlignment="Left" Margin="10,69,0,0" Name="r2" VerticalAlignment="Top" Width="120" Style="{StaticResource myRadioActivator2}">
</RadioButton>
</Grid>
</Window>
I have a comboBox which has the following items: a1, a2, a3, a4 and I have two RadioButtons r1 and r2.
This is what I want to accomplish:
if the user selects item a2 from the combobox the IsChecked property of r1 should be set to true. If the user selects either item a3 or a4 from the combobox the IsChecked propertyr of r2 should be set to true. I would like to accomplish this declaratively; i.e. without using a Converter.
Here is my Code and thanks in advance:
<Window x:Class="BMSystem.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<Style x:Key="myRadioActivator1">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Content, ElementName=comboBox1}" Value="R2">
<Setter Property="RadioButton.IsChecked" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
<Style x:Key="myRadioActivator2">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Content, ElementName=comboBox1}" Value="R3">
<Setter Property="RadioButton.IsChecked" Value="True"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Content, ElementName=comboBox1}" Value="R4">
<Setter Property="RadioButton.IsChecked" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<ComboBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" SelectionChanged="comboBox1_SelectionChanged">
<ComboBoxItem>R1</ComboBoxItem>
<ComboBoxItem>R2</ComboBoxItem>
<ComboBoxItem>R3</ComboBoxItem>
<ComboBoxItem>R4</ComboBoxItem>
</ComboBox>
<RadioButton Height="16" HorizontalAlignment="Left" Margin="10,43,0,0" Name="r1" VerticalAlignment="Top" Width="120" Style="{StaticResource myRadioActivator1}">
</RadioButton>
<RadioButton Height="16" HorizontalAlignment="Left" Margin="10,69,0,0" Name="r2" VerticalAlignment="Top" Width="120" Style="{StaticResource myRadioActivator2}">
</RadioButton>
</Grid>
</Window>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您在没有转换器的情况下执行此操作的目标是好的,但您完全以声明方式执行此操作的目标是值得怀疑的。我将一个
IsChecked
属性添加到ComboBox
项目的视图模型中并绑定到它。在我看来,将该属性设置背后的决策过程放入视图中似乎会混淆关注点的分离。I think your goal of doing this without a converter is good, but your goal of doing it entirely declaratively is questionable. I'd add an
IsChecked
property to the view model of theComboBox
's items and bind to it. Putting the decision-making process that underlies the setting of that property into the view seems, to me, to be muddying up the separation of concerns.伙计...这些都是一些奇怪的要求!
这是一种解决方案:
Man... those are some weird requirements!
Here is one solution:
您可以通过将所有内容移至
DataTemplate
并在其中使用Trigger
来实现。我可能会考虑 Robert 的建议,即在 ViewModel 或其他绑定对象中修复此问题,因为它听起来更像是业务逻辑而不仅仅是 UI。那是说:You could do it by moving everything into a
DataTemplate
and usingTrigger
s there. I would probably consider Robert's suggestion that this be fixed in aViewModel
or some other bound object because it sounds more like it might be more business logic than just UI. That said: