从 ItemsControl 获取单击的对象并使用其属性填充 Popup [英] Get clicked object from ItemsControl and populate Popup with its properties

查看:20
本文介绍了从 ItemsControl 获取单击的对象并使用其属性填充 Popup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ItemsControl,它从我的视图模型的列表中显示对象.当用户单击 ItemsControl 中的项目时,我还有代码来显示 Popup.但是我不知道如何从单击的项目中获取实际对象以读取其属性并将其显示在 Popup 中.

I've got an ItemsControl which displays objects from a list from my viewmodel. I also have code to display a Popup when the user click on an item in the ItemsControl. However I don't know how to get the actual object from the clicked item to read its properties and display them in the Popup.

我有一个用于 ButtonClick 事件处理程序(用于在 ItemsControl 中显示我的项目),我尝试了在调试器中查看按钮是否包含所需的对象,但显然没有.

I've got a Click event handler for the Button (which is used to display my items in the ItemsControl) and I tried to see in the debugger if the button contains the desired object but apparently it doesn't.

我还能如何获取对象并使用其属性填充弹出窗口?

How else can I get the object and populate the popup with its properties?

<ItemsControl ItemsSource="{Binding RecipientsNames}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button x:Name="btnConvoContact" Click="BtnConvoContact_Click"
                    Background="White" Foreground="Black" Cursor="Hand"
                    Width="Auto" Height="14" Padding="0" BorderThickness="0" Margin="0 0 6 0" HorizontalAlignment="Left" VerticalAlignment="Top">
                <TextBlock Text="{Binding Path=Name}" FontSize="12" Margin="0 -2 0 -2"/>
            </Button>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

推荐答案

将事件处理程序中 sender 参数的 DataContext 转换为您的数据类型:

Cast the DataContext of the sender argument in the event handler to your data type:

private void BtnConvoContact_Click(object sender, RoutedEventArgs e)
{
    Button btn = sender as Button;
    var dataObject = btn.DataContext as YourDataClass;
}

这篇关于从 ItemsControl 获取单击的对象并使用其属性填充 Popup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆