다음을 통해 공유


방법: RadioButton 선택 감지

업데이트: 2007년 11월

RadioButton 컨트롤을 그룹화하면 단추는 다른 항목과 함께 사용할 수 없게 됩니다. 사용자는 RadioButton 그룹 안의 항목을 한 번에 하나만 선택할 수 있습니다. 응용 프로그램에서는 사용자가 새 항목을 선택할 때 선택했던 항목을 프로그래밍 방식으로 지웁니다. RadioButton이 선택되는지 여부는 해당 IsChecked 속성의 상태에 따라 결정됩니다. RadioButton 컨트롤을 부모 내에 배치하거나 그룹 이름을 지정하여 그룹화할 수 있습니다. 다음 코드 예제에서는 두 가지 모두를 수행합니다. RadioButton 컨트롤은 StackPanel의 자식 요소이며 그룹 이름은 ExpandDirectionProperty입니다.

RadioButton을 선택하면 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;
}