2011-05-25 65 views

回答

0

一般來說,如果它只有一個RadioButtonGroup我建議你有一個默認的。因此,您不必驗證是否檢查了任何內容。

如果我們有具有多於2個選項我們通常用枚舉和轉換器,顯示在下面的示例中一個的RadioButtonGroup:

<StackPanel> 
    <RadioButton Content="Yes" 
       Padding="5,0" 
       IsChecked="{Binding Path=Existing, Mode=TwoWay, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter=Yes}" 
       GroupName="Existing" /> 
    <RadioButton Content="InProgress" 
       Margin="5,0" 
       Padding="5,0" 
       IsChecked="{Binding Path=Existing, Mode=TwoWay, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter=Pending}" 
       GroupName="Existing" /> 
    <RadioButton Content="No" 
       Margin="5,0" 
       Padding="5,0" 
       IsChecked="{Binding Path=Existing, Mode=TwoWay, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter=No}" 
       GroupName="Existing" /> 
</StackPanel> 

注意,現有的是一個枚舉並轉化成一個布爾值(EnumToBooleanConverter)通過使用System.Enum.Parse(value.GetType(),parameterString,true)。由於Enum不可空,所以四個RadioButton中的一個都被檢查!