Radiobuttonを使う WPFRadiobuttonを使う WPFRadiobuttonは、グループボックスなど、あるコントロールの中で使う。別にグループボックスの中だけではなく、単なるStackPanelの中でも、Gridの中でもOK。 GroupBoxの誤解しそうな所は、GroupBoxの中に直接、Radiobuttonを複数入れてしまい、エラーになるケース。 GroupBoxは子は一つしか持てません。 後は、Radiobuttonを使うのに手間取ることは無いでしょう。 選択されたか否かを取得するのも簡単です。 Radiobuttonが多くあった場合は、子要素をFor Eachで取得して回せば、選択コントロールも取得できるのでは? と思ってやってみると、このRadiobuttonって、System.Windows.Controls.RadioButtonであることが分かってびっくり。 何だこりゃ? でもま、使えればよいか? ![]()
<Window x:Class="RadioButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="RadioButton" Height="300" Width="300">
<Grid>
<StackPanel Margin="10">
<StackPanel.Resources>
<Style TargetType="RadioButton">
<Setter Property="Margin" Value="10"/>
</Style>
</StackPanel.Resources>
<RadioButton Name="A" Content="A" IsChecked="True"/>
<RadioButton Name="B" Content="B" />
<Button Content="Button" Height="23" Name="Button1" Width="75" />
<GroupBox Header="グループボックス" Padding="10">
<!--グループボックスの中には子は一つだけなのでコメントアウトの方法はだめ-->
<!--1<RadioButton Name="C" Content="C" IsChecked="True"/>
<RadioButton Name="D" Content="D" IsChecked="True"/>-->
<StackPanel Orientation="Horizontal" Name="Stack1">
<RadioButton Name="C" Content="C" IsChecked="True"/>
<RadioButton Name="D" Content="D"/>
</StackPanel>
</GroupBox>
</StackPanel>
</Grid>
</Window>
StackPaneを使うStackPaneは、他のコントロールの入れ物なので、RadioButtonの解説でついでに。上記のRadioButtonのXAML(ザムル)を見ると、見事に垂直に並んでます。 これって、中に入れるコントロールを、垂直または水平に簡単に配置できるものです。 Orientation="Vertical" で、垂直(デフォルト) Orientation="Horizontal" で水平、 ![]() です。 因みに、プロパティーを探すのに便利なのが、下記の図の赤く囲んだ箇所。 適当に打ち込むと、マッチしたプロパティーが出てきます。 便利だなーーーー。 ![]() |