开发者

RadioButton Data Validation in Silverlight 4

I have a form that contains four radio buttons that belong to the same group. The user has to check one of the buttons before clicking the开发者_如何学C submit button. Is there a way to make sure that the user has checked one radio button.


In general if it is only 1 RadioButtonGroup I would suggest you to have a default one. As a result you don't have to validate if anything is checked or not.

If we have a RadioButtonGroup with more than 2 options we usually use enums and converters as shown in the following example:

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

Note that Existing is an Enum and is converted to a boolean value (EnumToBooleanConverter) by using System.Enum.Parse(value.GetType(), parameterString, true). Since the Enum is not nullable there is one of the four RadioButtons checked all the time!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜