728x90
반응형
WPF & C# - 버튼을 라디오 버튼처럼 쓰기 ( RadioButton / Btn / Button / GroupButton / 그룹버튼 / 그룹 ) |
MainWindow.xaml
12345678910111213 <WrapPanel x:Name="stp"><Button Content="ON" Width="60" Height="40" Background="Yellow" Click="Button_Click" /><Button Content="Off" Width="60" Height="40" Click="Button_Click"/><Button Content="Off" Width="60" Height="40" Click="Button_Click"/><Button Content="Off" Width="60" Height="40" Click="Button_Click"/><Button Content="Off" Width="60" Height="40" Click="Button_Click"/><Button Content="Off" Width="60" Height="40" Click="Button_Click"/><Button Content="Off" Width="60" Height="40" Click="Button_Click"/><Button Content="Off" Width="60" Height="40" Click="Button_Click"/><Button Content="Off" Width="60" Height="40" Click="Button_Click"/><Button Content="Off" Width="60" Height="40" Click="Button_Click"/><Button Content="Off" Width="60" Height="40" Click="Button_Click"/></WrapPanel>cs
MainWindow.xaml.cs
123456789101112 private void Button_Click(object sender, RoutedEventArgs e){// 조건에 맞는 객체 찾기 (Content가 "ON"인 것)var selectBtn = stp.Children.OfType<Button>().FirstOrDefault(r => r.Content.ToString() == "ON");selectBtn.Content = "Off";selectBtn.Background = new SolidColorBrush(Colors.LightGray);// 선택한 객체 Content 변경하기Button btn = sender as Button;btn.Content = "ON";btn.Background = new SolidColorBrush(Colors.Yellow);}cs
728x90
반응형