0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

WPFで中身がClassアイテムのListBox.SelectedItemを取得する

Last updated at Posted at 2020-09-16

既出かもしれませんが備忘録として。

SelectedItems(複数選択)の取り出し方も書きました
https://qiita.com/Michio029/items/3b531acd46bb1f81f7d7

#準備
リストと中身を用意します
image.png

MainWindow.xaml
<Window><!-- Windowの細かいのは省略 -->
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="50"/>
        </Grid.RowDefinitions>
        
        <ListBox Grid.Row="0" Name="ExampleList" Margin="10" SelectionMode="Single" ScrollViewer.VerticalScrollBarVisibility="Auto">
            <!-- SelectionMode="Single" :リストから一つしか選択できない -->
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Id, StringFormat=IDは{0} :}"/>
                        <TextBlock Text="{Binding Name, StringFormat= Nameは{0} :}"/>
                        <TextBlock Text="{Binding Age, StringFormat= Ageは{0}}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Button Grid.Row="1" Name="Btn" Content="ボタン" Margin="100,10,100,10" Click="Btn_Click"/>
    </Grid>
</Window>

<TextBlock Text="{Binding Id, StringFormat=IDは{0} :}"/>
この辺の文字列のフォーマットの書き方についてはこちらで詳しく解説してくれています。
https://qiita.com/koara-local/items/815eb5146b3ddc48a8c3

MainWindow.xaml.cs
public partial class MainWindow : Window
{
    List<ExampleClass> list = new List<ExampleClass>();
    public MainWindow()
    {
        InitializeComponent();

        list.Add(new ExampleClass() { Id = 0, Name = "aaa", Age = 10 });
        list.Add(new ExampleClass() { Id = 1, Name = "bbb", Age = 20 });
        list.Add(new ExampleClass() { Id = 2, Name = "ccc", Age = 30 });
        list.Add(new ExampleClass() { Id = 3, Name = "ddd", Age = 40 });
        list.Add(new ExampleClass() { Id = 4, Name = "eee", Age = 50 });
        ExampleList.ItemsSource = list;
    }

    private void Btn_Click(object sender, RoutedEventArgs e)
    {

    }
}
class ExampleClass
{
    public int Id { get; set; }
    public string Name { get; set; }
    public byte Age { get; set; }
}

#SelectedItemを取り出す
ボタンの処理を追記します。

    private void Btn_Click(object sender, RoutedEventArgs e)
    {
        // 選択項目が0 => メソッドを出る
        if (ExampleList.SelectedItems.Count == 0)
            return;

        // ExampleClassとして取り出す
        ExampleClass selitem = ExampleList.SelectedItem as ExampleClass;

        // 中身の確認
        Console.WriteLine("選択中の項目 Id:{0} Name:{1} Age{2}", selitem.Id, selitem.Name, selitem.Age);
    }

ボタンを押してみます ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/301115/88dfa187-f4f6-c9fe-694a-c636f288e7a3.png)

実行結果

選択中の項目 Id:0 Name:aaa Age10

取り出せました。

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?