带有ContextMenu的wpf listView没有使用数据绑定显示数据

时间:2018-02-21 16:56:00

标签: c# wpf data-binding

我的列表视图存在问题。

它显示了我添加到与其绑定的ObservableCollection的所有元素,以及它应该如何工作,但是当我右键单击它的任何元素时,绑定会赢得'工作,它不会像我打算那样显示数据。

我创建了另一个WPF项目,以更清楚地向您展示问题。

这是我的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>

和它背后的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; }

}

}

这就是我得到的:

Result

1 个答案:

答案 0 :(得分:1)

ContextMenu与ListView(或任何其他控件)不在同一个可视树中。它与Window元素树完全分开,这就是它在绑定时丢失的原因。

我得到的解决方案可能不是最漂亮的,但有效:)

将ContextMenuOpening事件设置为ListView:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style>
        html, body { height:100%; /*Can also use 100vh*/ }

        div {
            min-width: 300px;
            max-width: 500px;
            min-height: 300px;
            max-height: 500px;
            background-color: Pink;
            height: 100%;
        }
        p {
            width: 75%;
            height: 75%;
            background-color: rgb(0,20,80);
        }
    </style>
</head>
<body>
    <div><p>Paragraph of text...</p></div>
</body>
</html>

在你的代码隐藏中,执行:

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