从 ItemsControl 获取单击的对象并使用其属性填充 Popup

2023-12-10

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

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

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

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

投下DataContext of the sender事件处理程序中的参数为您的数据类型:

private void BtnConvoContact_Click(object sender, RoutedEventArgs e)
{
    Button btn = sender as Button;
    var dataObject = btn.DataContext as YourDataClass;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 ItemsControl 获取单击的对象并使用其属性填充 Popup 的相关文章

随机推荐