C# 如何声明性地从组合框的内容设置RadioButton的IsChecked属性?

C# 如何声明性地从组合框的内容设置RadioButton的IsChecked属性?,c#,wpf,combobox,radio-button,datatrigger,C#,Wpf,Combobox,Radio Button,Datatrigger,我有一个组合框,它有以下项目:a1、a2、a3、a4,我有两个单选按钮r1和r2。 这就是我想要实现的目标: 如果用户从组合框中选择项目a2,则r1的IsChecked属性应设置为true。如果用户从组合框中选择项目a3或a4,则r2的IsChecked propertyr应设置为true。我想以宣言的方式完成这一点;i、 e.不使用转换器。 这是我的代码,请提前感谢: <Window x:Class="BMSystem.Window1" xmlns="http://schemas

我有一个组合框,它有以下项目:a1、a2、a3、a4,我有两个单选按钮r1和r2。 这就是我想要实现的目标: 如果用户从组合框中选择项目a2,则r1的IsChecked属性应设置为true。如果用户从组合框中选择项目a3或a4,则r2的IsChecked propertyr应设置为true。我想以宣言的方式完成这一点;i、 e.不使用转换器。 这是我的代码,请提前感谢:

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

R1
R2
R3
R4

我认为您在不使用转换器的情况下实现这一目标是好的,但您完全以声明方式实现这一目标的目标值得怀疑。我会将一个
IsChecked
属性添加到
组合框
项的视图模型中并绑定到它。在我看来,将作为该属性设置基础的决策过程放到视图中似乎是在混淆关注点的分离。

我认为您在没有转换器的情况下这样做的目标是好的,但您完全以声明方式这样做的目标是值得怀疑的。我会将一个
IsChecked
属性添加到
组合框
项的视图模型中并绑定到它。在我看来,将作为该财产设置基础的决策过程纳入视图似乎正在混淆关注点的分离。。。这是一些奇怪的要求

这里有一个解决方案:

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

男人。。。这是一些奇怪的要求

这里有一个解决方案:

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

您可以通过将所有内容移动到
数据模板中,并使用
触发器来实现。我可能会认为罗伯特的建议是固定在<代码> VIEWSMODS或其他绑定对象中,因为它听起来更像是业务逻辑而不是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>

R1
R2
R3
R4

您可以通过将所有内容移动到
数据模板中,并使用
触发器来实现。我可能会认为罗伯特的建议是固定在<代码> VIEWSMODS或其他绑定对象中,因为它听起来更像是业务逻辑而不是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>

R1
R2
R3
R4

你能把
comboBox1\u-selection-changed
处理程序也贴出来吗?你能把
comboBox1\u-selection-changed
处理程序也贴出来吗。