私はC#とWPFを使用していますが、基本的にいくつかのトグルボタンが必要で、同時に選択できるのは1つだけです。
それについて別の質問を見つけましたが、そこに示されている解決策は機能せず、理由がわかりません。
上記の質問にあるようにしようとすると、ItemTemplate
のListBox
が適用されません。トグルボタンをリストボックスに入れず、代わりに「通常のリストボックス」として表示します。
トグル ボタンのスタイルは次のようになり、リソース ファイルの 1 つに含まれています。
<Style x:Key="ToggleButtonListBox" TargetType="{x:Type ListBox}">
<Setter Property="ListBox.ItemTemplate">
<Setter.Value>
<DataTemplate>
<ToggleButton Content="{Binding}"
IsChecked="{Binding IsSelected, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" />
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="ListBox.ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="BorderThickness" Value="0" />
</Style>
アイテムを XAML コードに直接追加したいので、そのためのコードは次のようになります
<ListBox Style="{StaticResource ToggleButtonListBox}">
<ListBoxItem>test1</ListBoxItem>
<ListBoxItem>test2</ListBoxItem>
</ListBox>
このようなボタンのセットを作成するにはどうすればよいですか?