如何以声明方式从 ComboBox 的内容中设置 RadioButton 的 IsChecked 属性?

发布于 2024-08-20 15:56:55 字数 2340 浏览 3 评论 0原文

我有一个组合框,其中包含以下项目: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

影子是时光的心 2024-08-27 15:56:55

我认为您在没有转换器的情况下执行此操作的目标是好的,但您完全以声明方式执行此操作的目标是值得怀疑的。我将一个 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 the ComboBox'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.

℉絮湮 2024-08-27 15:56:55

伙计...这些都是一些奇怪的要求!

这是一种解决方案:

<Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
  <Page.Resources> 
    <XmlDataProvider x:Key="CBOptions">
      <x:XData>
        <Data xmlns="">
          <Option Name="R1" />
          <Option Name="R2" IsR1Checked="True" />
          <Option Name="R3" IsR2Checked="True" />
          <Option Name="R4" IsR2Checked="True" />
        </Data>
      </x:XData>
    </XmlDataProvider>
    <DataTemplate x:Key="CBItemTemplate">
      <TextBlock Text="{Binding XPath=@Name}" />
    </DataTemplate>
  </Page.Resources> 
  <Grid DataContext="{Binding ElementName=comboBox1, Path=SelectedItem}"> 
    <ComboBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="comboBox1" 
        VerticalAlignment="Top" Width="120" IsSynchronizedWithCurrentItem="True"
        ItemsSource="{Binding Source={StaticResource CBOptions}, XPath=Data/Option}"
        ItemTemplate="{StaticResource CBItemTemplate}" />
    <RadioButton Height="16" HorizontalAlignment="Left" Margin="10,43,0,0" 
        Name="r1" VerticalAlignment="Top" Width="120" GroupName="R1" 
        IsChecked="{Binding XPath=@IsR1Checked}" />
    <RadioButton Height="16" HorizontalAlignment="Left" Margin="10,69,0,0" 
        Name="r2" VerticalAlignment="Top" Width="120" GroupName="R2" 
        IsChecked="{Binding XPath=@IsR2Checked}" />
  </Grid> 
</Page> 

Man... those are some weird requirements!

Here is one solution:

<Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
  <Page.Resources> 
    <XmlDataProvider x:Key="CBOptions">
      <x:XData>
        <Data xmlns="">
          <Option Name="R1" />
          <Option Name="R2" IsR1Checked="True" />
          <Option Name="R3" IsR2Checked="True" />
          <Option Name="R4" IsR2Checked="True" />
        </Data>
      </x:XData>
    </XmlDataProvider>
    <DataTemplate x:Key="CBItemTemplate">
      <TextBlock Text="{Binding XPath=@Name}" />
    </DataTemplate>
  </Page.Resources> 
  <Grid DataContext="{Binding ElementName=comboBox1, Path=SelectedItem}"> 
    <ComboBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="comboBox1" 
        VerticalAlignment="Top" Width="120" IsSynchronizedWithCurrentItem="True"
        ItemsSource="{Binding Source={StaticResource CBOptions}, XPath=Data/Option}"
        ItemTemplate="{StaticResource CBItemTemplate}" />
    <RadioButton Height="16" HorizontalAlignment="Left" Margin="10,43,0,0" 
        Name="r1" VerticalAlignment="Top" Width="120" GroupName="R1" 
        IsChecked="{Binding XPath=@IsR1Checked}" />
    <RadioButton Height="16" HorizontalAlignment="Left" Margin="10,69,0,0" 
        Name="r2" VerticalAlignment="Top" Width="120" GroupName="R2" 
        IsChecked="{Binding XPath=@IsR2Checked}" />
  </Grid> 
</Page> 
花海 2024-08-27 15:56:55

您可以通过将所有内容移至 DataTemplate 并在其中使用 Trigger 来实现。我可能会考虑 Robert 的建议,即在 ViewModel 或其他绑定对象中修复此问题,因为它听起来更像是业务逻辑而不仅仅是 UI。那是说:

<ContentControl Content="{Binding}">
    <ContentControl.ContentTemplate>
        <DataTemplate>
            <Grid>
                <ComboBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" SelectionChanged="comboBox1_SelectionChanged"
                          SelectedValuePath="Content">
                    <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" >
                </RadioButton>
                <RadioButton Height="16" HorizontalAlignment="Left" Margin="10,69,0,0" Name="r2" VerticalAlignment="Top" Width="120" >
                </RadioButton>
            </Grid>
            <DataTemplate.Triggers>
                <Trigger SourceName="comboBox1" Property="SelectedValue" Value="R2">
                    <Setter TargetName="r1" Property="RadioButton.IsChecked" Value="True"/>
                </Trigger>
                <Trigger SourceName="comboBox1" Property="SelectedValue" Value="R3">
                    <Setter TargetName="r2" Property="RadioButton.IsChecked" Value="True"/>
                </Trigger>
                <Trigger SourceName="comboBox1" Property="SelectedValue" Value="R4">
                    <Setter TargetName="r2" Property="RadioButton.IsChecked" Value="True"/>
                </Trigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </ContentControl.ContentTemplate>
</ContentControl>

You could do it by moving everything into a DataTemplate and using Triggers there. I would probably consider Robert's suggestion that this be fixed in a ViewModel or some other bound object because it sounds more like it might be more business logic than just UI. That said:

<ContentControl Content="{Binding}">
    <ContentControl.ContentTemplate>
        <DataTemplate>
            <Grid>
                <ComboBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" SelectionChanged="comboBox1_SelectionChanged"
                          SelectedValuePath="Content">
                    <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" >
                </RadioButton>
                <RadioButton Height="16" HorizontalAlignment="Left" Margin="10,69,0,0" Name="r2" VerticalAlignment="Top" Width="120" >
                </RadioButton>
            </Grid>
            <DataTemplate.Triggers>
                <Trigger SourceName="comboBox1" Property="SelectedValue" Value="R2">
                    <Setter TargetName="r1" Property="RadioButton.IsChecked" Value="True"/>
                </Trigger>
                <Trigger SourceName="comboBox1" Property="SelectedValue" Value="R3">
                    <Setter TargetName="r2" Property="RadioButton.IsChecked" Value="True"/>
                </Trigger>
                <Trigger SourceName="comboBox1" Property="SelectedValue" Value="R4">
                    <Setter TargetName="r2" Property="RadioButton.IsChecked" Value="True"/>
                </Trigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </ContentControl.ContentTemplate>
</ContentControl>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文