LoginSignup
0
3

More than 3 years have passed since last update.

WPF RadioButtonの使い方 覚え書き

Last updated at Posted at 2018-12-04

ListBoxをRadioButtonで表現する

よくやり方を忘れるので覚え書きです。

とりあえずはBinding用のクラスを用意する

Item1.cs
public class Item1
{
  public int Id {get; set;}
  public string ItemName {get; set;}
}

ViewModelを用意
PrismとReactivePropertyを使用しているがここでは省略

Item1ListViewModel.cs
public class  Item1ListViewModel:BindbleBase
{
  public ReacitiveCollection<Item1> Items {get;} = new ReactiveCollection<Item1>();
  public ReactiveProperty<int> SelectedItem1 {get;} = new ReactiveProperty<int>(0);

  public Item1ListViewModel()
  {
     Items.AddRangeOnScheduler(
         new Item1 { Id = 0, ItemName = "ABC" },
         new Item1 { Id = 1, ItemName = "AAA" },
         new Item1 { Id = 2, ItemName = "BBB" },
         new Item1 { Id = 3, ItemName = "CCC" }
     );
  }
}

Viewを用意
今回は前後略でListBoxを記載する

<ListBox ItemsSource="{Binding Items}"
         SelectionMode="Single"
         SelectedValuePath="Id"
         SelectedValue="{Binding SelectedItem1.Value}">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
           <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemContainerStyle>
       <Style TargetType="ListBoxItem">
          <Setter Property="Template">
              <Setter.Value>
                 <ControlTemplate>
                     <RadioButton Margin="3"
                                  Content="{Binding ItemName}"
                                  IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected}"/>
                 </ControlTemplate>
              </Setter.Value>
          </Setter>
       </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

これでとりあえずはRadioButtonが横並びに表示されます。
より詳しくは下記のブログから参照できます
参考URL : RadioButton を ListBox で実装する(frog.raindrop.jp.knowledge)

0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3