ボタンを横に並べ、
トグルボタンのように動作させたいと思っている。
できたら、どのボタンを押した状態なのかも把握したい。
Zuishinさんが書いておられますが、トグルボタンは既にあります。
ToggleButton クラス (System.Windows.Controls.Primitives) | Microsoft Docs
わかっておられるでしょうが、これはCheckBox
のボタン版です。
それぞれにOn・Off状態があります。
そうではなくRadioButton
のボタン版が欲しい場合は、スタイルをToggleButton
にするという大技?があります。
どちらもボタンの見た目な場合は、ThreeState
は設定しないほうがいいでしょう。
まったく違いが判りません(自分でスタイルをいじる場合はその限りではありません)
ボタンをクリックした場合、BackGroundまたはBorderを黄色にする
クリックされなかったボタンはデフォルト色にする
スタイルをいじればどうとでもなりますが、何か理想の見た目がありますか?
xml
1<Window
2 x:Class="Questions353667.MainWindow"
3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5 SizeToContent="WidthAndHeight">
6 <StackPanel>
7 <GroupBox Header="CheckBox">
8 <StackPanel Orientation="Horizontal">
9 <CheckBox
10 MinWidth="50"
11 Content="C1"
12 IsChecked="True" />
13 <CheckBox
14 MinWidth="50"
15 Content="C2"
16 IsChecked="True" />
17 <CheckBox MinWidth="50" Content="C3" />
18 <CheckBox
19 MinWidth="50"
20 Content="C4"
21 IsChecked="{x:Null}"
22 IsThreeState="True" />
23 </StackPanel>
24 </GroupBox>
25 <GroupBox Header="ToggleButton">
26 <StackPanel Orientation="Horizontal">
27 <ToggleButton
28 MinWidth="50"
29 Content="B1"
30 IsChecked="True" />
31 <ToggleButton
32 MinWidth="50"
33 Content="B2"
34 IsChecked="True" />
35 <ToggleButton MinWidth="50" Content="B3" />
36 <ToggleButton
37 MinWidth="50"
38 Content="B4"
39 IsChecked="{x:Null}"
40 IsThreeState="True" />
41 </StackPanel>
42 </GroupBox>
43 <GroupBox Header="RadioButton">
44 <StackPanel Orientation="Horizontal">
45 <RadioButton
46 MinWidth="50"
47 Content="R1"
48 IsChecked="True" />
49 <RadioButton MinWidth="50" Content="R2" />
50 <RadioButton MinWidth="50" Content="R3" />
51 <RadioButton
52 MinWidth="50"
53 Content="R4"
54 IsChecked="{x:Null}"
55 IsThreeState="True" />
56 </StackPanel>
57 </GroupBox>
58 <GroupBox Header="RadioButton(Button Style)">
59 <StackPanel Orientation="Horizontal">
60 <RadioButton
61 MinWidth="50"
62 Content="B1"
63 IsChecked="True"
64 Style="{StaticResource {x:Type ToggleButton}}" />
65 <RadioButton
66 MinWidth="50"
67 Content="B2"
68 Style="{StaticResource {x:Type ToggleButton}}" />
69 <RadioButton
70 MinWidth="50"
71 Content="B3"
72 Style="{StaticResource {x:Type ToggleButton}}" />
73 <RadioButton
74 MinWidth="50"
75 Content="B4"
76 IsChecked="{x:Null}"
77 IsThreeState="True"
78 Style="{StaticResource {x:Type ToggleButton}}" />
79 </StackPanel>
80 </GroupBox>
81 </StackPanel>
82</Window>
