「東雲 忠太郎」の平凡な日常のできごと

「東雲 忠太郎」の平凡な日常のできごと

2024.04.20
XML
カテゴリ: C#.NET


Buttonのイベントを追加します

private void Button_Click(object sender, RoutedEventArgs e)

{

    // イベントを発生させたボタンを取得

    Button button = sender as Button;


    // ボタンが含まれる ListViewItem を取得

    ListViewItem listViewItem = FindAncestor<ListViewItem>(button);


    // ListViewItem にバインドされたデータアイテムを取得

    Item item = listViewItem.DataContext as Item;


    // データアイテムが ObservableCollection 内のどの位置にあるかを調べる

    int index = Items.IndexOf(item);


    // index を使って何かを行う

    MessageBox.Show($"Item at index {index} clicked.");

}




// VisualTree 上で指定された型の親要素を検索するヘルパーメソッド

private static T FindAncestor<T>(DependencyObject current) where T : DependencyObject

{

    do

    {

        if (current is T)

        {

            return (T)current;

        }

        current = VisualTreeHelper.GetParent(current);

    }

    while (current != null);

    return null;

}


<Window x:Class="WpfApp1.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="MainWindow" Height="450" Width="800">

    <Grid>

        <ListView Name="listView" HorizontalAlignment="Left" Height="400" VerticalAlignment="Top" Width="300">

            <ListView.View>

                <GridView>

                    <GridViewColumn Header="Item Name" Width="150">

                        <GridViewColumn.CellTemplate>

                            <DataTemplate>

                                <TextBox Text="{Binding Name}" />

                            </DataTemplate>

                        </GridViewColumn.CellTemplate>

                    </GridViewColumn>

                    <GridViewColumn Header="Action" Width="150">

                        <GridViewColumn.CellTemplate>

                            <DataTemplate>

                                <Button Content="Click Me" Click="Button_Click"/>

                            </DataTemplate>

                        </GridViewColumn.CellTemplate>

                    </GridViewColumn>

                </GridView>

            </ListView.View>

        </ListView>

    </Grid>

</Window>


using System;

using System.Collections.Generic;

using System.Collections.ObjectModel;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;


namespace WpfApp1

{

    public partial class MainWindow : Window

    {

        public ObservableCollection<Item> Items { get; set; }


        public MainWindow()

        {

            InitializeComponent();

            Items = new ObservableCollection<Item>();

            // データを追加して ListView に表示する

            Items.Add(new Item { Name = "Item 1" });

            Items.Add(new Item { Name = "Item 2" });

            listView.ItemsSource = Items;

        }


        // ボタンがクリックされたときのイベントハンドラ

        private void Button_Click(object sender, RoutedEventArgs e)

        {

            // イベントを発生させたボタンを取得

            Button button = sender as Button;


            // ボタンが含まれる ListViewItem を取得

            ListViewItem listViewItem = FindAncestor<ListViewItem>(button);


            // ListViewItem にバインドされたデータアイテムを取得

            Item item = listViewItem.DataContext as Item;


            // データアイテムが ObservableCollection 内のどの位置にあるかを調べる

            int index = Items.IndexOf(item);


            // index を使って何かを行う

            MessageBox.Show($"Item at index {index} clicked.");

        }


        // VisualTree 上で指定された型の親要素を検索するヘルパーメソッド

        private static T FindAncestor<T>(DependencyObject current) where T : DependencyObject

        {

            do

            {

                if (current is T)

                {

                    return (T)current;

                }

                current = VisualTreeHelper.GetParent(current);

            }

            while (current != null);

            return null;

        }

    }


    public class Item

    {

        public string Name { get; set; }

    }

}






お気に入りの記事を「いいね!」で応援しよう

Last updated  2024.04.20 07:08:28


【毎日開催】
15記事にいいね!で1ポイント
10秒滞在
いいね! -- / --
おめでとうございます!
ミッションを達成しました。
※「ポイントを獲得する」ボタンを押すと広告が表示されます。
x

© Rakuten Group, Inc.
X
Create a Mobile Website
スマートフォン版を閲覧 | PC版を閲覧
Share by: