我有一個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>
你可以發佈'comboBox1_SelectionChanged'處理程序請。 – ChrisF 2010-02-06 16:56:18