从ItemScontrol获得单击对象,并填充其属性



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

我有一个ClickButton事件处理程序(用于在ItemsControl中显示我的项目),我尝试在调试器中查看按钮是否包含所需的对象,但显然不是。p>我还能如何获取对象并填充其属性?

<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铸造为您的数据类型:

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

最新更新