首页 > 解决方案 > 如何在 WPF C# 中的 ItemsControl 中获取项目值?

问题描述

在此处输入图像描述单击 ItemsControl 项目时,我想从 ItemsControl 获取项目值。

下面是我的示例代码:

 <ItemsControl x:Name="ListViewProducts" ItemsSource="{Binding Product}"  PreviewMouseLeftButtonUp="listView_Click" >
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Border Margin="10" Width="110" Height="160">
                                    <StackPanel>
                                        <Border Width="100" Height="100" CornerRadius="5" Margin="5">
                                            <Border.Effect>
                                                <DropShadowEffect ShadowDepth="1"/>
                                            </Border.Effect>
                                            <Border.Background>
                                                <ImageBrush ImageSource="{Binding Image}" />
                                            </Border.Background>
                                        </Border>
                                        <!--<TextBlock Margin="5" Text="{Binding Value, StringFormat={}{0:C}}" FontSize="17" FontFamily="Franklin Gothic Medium"/>-->
                                        <TextBlock Margin="5 0" Text="{Binding Name}" FontSize="15"/>
                                    </StackPanel>
                                </Border>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <WrapPanel/>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                    </ItemsControl>

代码隐藏:

private void listView_Click(object sender, RoutedEventArgs e)
    {
        var item = (sender as ItemsControl);

        if (item != null)
        {

        }


    }

但这不起作用,我没有得到项目的详细信息。请帮助我。

我只需要使用 Itemcontrol。当我使用 listview 时,它显示列表不是正确的方式,所以我使用了 itemscontrol。请看下面的图片。当我使用列表视图时,它显示为一个框。请找到截图。

在此处输入图像描述

标签: c#sqlasp.net.netwpf

解决方案


您可以定义一个并处理容器的事件,PreviewMouseLeftButtonUp而不是处理。然后,您可以简单地将参数转换为您的类型:ItemsControlItemContainerStylePreviewMouseLeftButtonDownDataContextsender

private void ContentControl_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    var product = ((FrameworkElement)sender).DataContext as Product;
    ...
}

XAML:

<ItemsControl x:Name="ListViewProducts" ItemsSource="{Binding Product}" >
    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
            <EventSetter Event="PreviewMouseLeftButtonDown" Handler="ContentControl_PreviewMouseLeftButtonDown" />
        </Style>
    </ItemsControl.ItemContainerStyle>
    <ItemsControl.ItemTemplate>
        ...
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemsPanel>
        ...
    </ItemsControl.ItemsPanel>
</ItemsControl>

或者为in处理PreviewMouseLeftButtonDown事件并做同样的事情。BorderItemTemplate


推荐阅读