2
我有一個包含屬於同一組的四個單選按鈕的表單。用戶在點擊提交按鈕之前必須檢查其中一個按鈕。有沒有辦法確保用戶已經選中了一個單選按鈕。Silverlight 4中的RadioButton數據驗證
我有一個包含屬於同一組的四個單選按鈕的表單。用戶在點擊提交按鈕之前必須檢查其中一個按鈕。有沒有辦法確保用戶已經選中了一個單選按鈕。Silverlight 4中的RadioButton數據驗證
一般來說,如果它只有一個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中的一個都被檢查!