方法 : RadioButton の選択内容を検出する
更新 : 2007 年 11 月
RadioButton コントロールをグループ化すると、複数のボタンを同時に選択できなくなります。RadioButton グループ内の項目は一度に 1 つしか選択できません。選択された項目は、ユーザーが別の項目を選択したときに自動的にクリアされます。RadioButton が選択されているかどうかは、その IsChecked プロパティの状態によって判断されます。RadioButton コントロールをグループ化するには、それらのコントロールを親の内部に配置するか、グループ名を付けます。次のコード例では、この両方を実行しています。RadioButton コントロールは StackPanel の子要素で、グループ名は ExpandDirectionProperty です。
RadioButton の 1 つが選択されると、Checked イベントが発生します。次のコード例に示すように、RadioButton の選択項目が変更されたときにアプリケーションでなんらかのアクションを実行する必要がある場合は、Checked イベントを処理するイベント ハンドラを追加します。
使用例
<StackPanel>
<RadioButton Name="ExpandDown" Margin="0,10,0,10"
IsChecked="True"
Checked="ChangeExpandDirection"
GroupName="ExpandDirectionProperty">
Expand Down
</RadioButton>
<RadioButton Name="ExpandUp" Margin="0,0,0,10"
Checked="ChangeExpandDirection"
GroupName="ExpandDirectionProperty">
Expand Up
</RadioButton>
<RadioButton Name="ExpandLeft" Margin="0,0,0,10"
Checked="ChangeExpandDirection"
GroupName="ExpandDirectionProperty">
Expand Left
</RadioButton>
<RadioButton Name="ExpandRight" Margin="0,0,0,10"
Checked="ChangeExpandDirection"
GroupName="ExpandDirectionProperty">
Expand Right
</RadioButton>
</StackPanel>
Private Sub ChangeExpandDirection(ByVal Sender As Object, ByVal e As RoutedEventArgs)
If (ExpandDown.IsChecked) Then
myExpander.ExpandDirection = ExpandDirection.Down
ElseIf (ExpandUp.IsChecked) Then
myExpander.ExpandDirection = ExpandDirection.Up
ElseIf (ExpandLeft.IsChecked) Then
myExpander.ExpandDirection = ExpandDirection.Left
ElseIf (ExpandRight.IsChecked) Then
myExpander.ExpandDirection = ExpandDirection.Right
End If
'Expand myExpander so it is easier to see the effect of changing
'the ExpandDirection property for My Expander
myExpander.IsExpanded = True
End Sub
private void ChangeExpandDirection(object sender, RoutedEventArgs e)
{
if ((Boolean)ExpandDown.IsChecked)
myExpander.ExpandDirection = ExpandDirection.Down;
else if ((Boolean)ExpandUp.IsChecked)
myExpander.ExpandDirection = ExpandDirection.Up;
else if ((Boolean)ExpandLeft.IsChecked)
myExpander.ExpandDirection = ExpandDirection.Left;
else if ((Boolean)ExpandRight.IsChecked)
myExpander.ExpandDirection = ExpandDirection.Right;
//Expand myExpander so it is easier to see the effect of changing
//the ExpandDirection property for My Expander
myExpander.IsExpanded = true;
}