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

时间:2010-02-06 16:18:06

标签: c# wpf combobox radio-button datatrigger

我有一个comboBox,其中包含以下项目:a1,a2,a3,a4和我有两个RadioButtons 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>

3 个答案:

答案 0 :(得分:1)

我认为你在没有转换器的情况下实现这一目标的目标是好的,但是你完全以声明方式完成它的目标是值得怀疑的。我将IsChecked属性添加到ComboBox项的视图模型中并绑定到它。对我来说,将作为该财产背景的基础的决策过程纳入观点似乎是混淆了关注点的分离。

答案 1 :(得分:0)

男人......那些是一些奇怪的要求!

这是一个解决方案:

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

答案 2 :(得分:0)

您可以将所有内容移动到DataTemplate并在那里使用Trigger来实现。我可能会考虑罗伯特的建议,即在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>