简体   繁体   English

wpf listView with ContextMenu 不使用数据绑定显示数据

[英]wpf listView with ContextMenu not showing data using data-binding

I have a problem with my List View.我的列表视图有问题。

It shows all the elements that I add to the ObservableCollection binded to it, just how it's supposed to work, but when I right-click any of it's elements, the bindings won't work and it won't display the data as I intend it to do.它显示了我添加到绑定到它的 ObservableCollection 的所有元素,以及它应该如何工作,但是当我右键单击它的任何元素时,绑定将不起作用并且它不会按我的意图显示数据它要做。

I created another WPF project to show you the problem more clearly.我创建了另一个 WPF 项目来更清楚地向您展示问题。

Here's my wpf code:这是我的 wpf 代码:

<Window x:Class="WpfApp2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <ListView x:Name="listViewWithContextMenu" ItemsSource="{Binding  Path=CollectionOfThings}"  HorizontalAlignment="Stretch"  VerticalAlignment="Stretch">
        <ListView.View>
            <GridView>
                <GridViewColumn Width="120" Header="Name" DisplayMemberBinding="{Binding Name}"/>
                <GridViewColumn Width="120" Header="Quantity" DisplayMemberBinding="{Binding Quantity}"/>
            </GridView>
        </ListView.View>
        <ListView.ContextMenu>
            <ContextMenu>
                <StackPanel Orientation="Horizontal">
                    <StackPanel Orientation="Vertical" Margin="3">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="Name: "></TextBlock>
                            <TextBlock Text="{Binding Name}"></TextBlock>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="Quantity: "></TextBlock>
                            <TextBlock Text="{Binding Quantity}"></TextBlock>
                        </StackPanel>
                    </StackPanel>
                </StackPanel>
            </ContextMenu>
        </ListView.ContextMenu>
    </ListView>
</Grid>

and the c# code behind it:及其背后的c#代码:

using System.Windows;
using System.Collections.ObjectModel;

namespace WpfApp2
{
public partial class MainWindow : Window
{
    public ObservableCollection<DataOfThing> CollectionOfThings = new ObservableCollection<DataOfThing>();
    public MainWindow()
    {
        InitializeComponent();

        CollectionOfThings.Add(new DataOfThing() { Name = "Some Name", Quantity = 2 });
        CollectionOfThings.Add(new DataOfThing() { Name = "Some Other Name", Quantity = 3 });
        CollectionOfThings.Add(new DataOfThing() { Name = "Strange Name", Quantity = 1 });

        listViewWithContextMenu.ItemsSource = CollectionOfThings;
    }
}

public class DataOfThing
{
    public string Name { get; set; }
    public int Quantity { get; set; }

}

} }

And here's what I get:这就是我得到的:

结果

What happens is that ContextMenu is not in the same visual tree of your ListView (or any other control).发生的情况是 ContextMenu 不在 ListView(或任何其他控件)的同一可视树中。 It is completely separated from your Window element tree and that's why it gets lost on binding.它与您的 Window 元素树完全分离,这就是它在绑定时丢失的原因。

I got a solution that might not be the most beautiful but works :)我得到了一个可能不是最漂亮但有效的解决方案:)

Set a ContextMenuOpening event to your ListView:将 ContextMenuOpening 事件设置为您的 ListView:

<ListView x:Name="listViewWithContextMenu" ItemsSource="{Binding Path=CollectionOfThings}"  HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" ContextMenuOpening="listViewWithContextMenu_ContextMenuOpening">

And in your codebehind, do:在你的代码隐藏中,做:

private void listViewWithContextMenu_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
    var list = sender as ListView;
    list.ContextMenu.DataContext = list.SelectedItem;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM