2010-02-06 41 views
1

我有一個comboBox有以下幾項:a1,a2,a3,a4和我有兩個RadioButtons r1和r2。 這是我想要完成的: 如果用戶從組合框中選擇了項目a2,則應將r1的IsChecked屬性設置爲true。如果用戶從組合框中選擇a3或a4項,則應將r2的IsChecked屬性設置爲true。我想以聲明的方式完成這件事。即不使用轉換器。 這裏是我的代碼,並在此先感謝:如何聲明性地從ComboBox的內容中設置RadioButton的IsChecked屬性?

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

你可以發佈'comboBox1_SelectionChanged'處理程序請。 – ChrisF 2010-02-06 16:56:18

回答

1

我覺得你這樣做,無需轉換器的目的是好的,但你這樣做完全是聲明的目標是值得商榷的。我將IsChecked屬性添加到ComboBox的項目的視圖模型並綁定到它。對於我來說,將設置該財產作爲基礎的決策過程看​​起來似乎混淆了關注的分離。

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 [email protected]}" /> 
    </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 [email protected]}" /> 
    <RadioButton Height="16" HorizontalAlignment="Left" Margin="10,69,0,0" 
     Name="r2" VerticalAlignment="Top" Width="120" GroupName="R2" 
     IsChecked="{Binding [email protected]}" /> 
    </Grid> 
</Page> 
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>