C# ListView 如何以编程方式创建 ContextMenu

问题描述 投票:0回答:2

我背后有这个代码:

private void InitializeListView()
    {
        //RAZ
        lv.Items.Clear();

        GridView gridView = new GridView();
        gridView.AllowsColumnReorder = true;
        
        GridViewColumn gvc1 = new GridViewColumn();
        gvc1.DisplayMemberBinding = new System.Windows.Data.Binding("SN");
        gvc1.Header = "SN";

        GridViewColumn gvc2 = new GridViewColumn();
        gvc2.DisplayMemberBinding = new System.Windows.Data.Binding("a1");
        gvc2.Header = "A1";
        gridView.Columns.Add(gvc2);

        GridViewColumn gvc3 = new GridViewColumn();
        gvc3.DisplayMemberBinding = new System.Windows.Data.Binding("a2");
        gvc3.Header = "A2";
        gridView.Columns.Add(gvc3);

        for (int i = 0; i < lv.Count; i++)
        {
            this.lv.Items.Add(
                new dataToUse
                {
                    sn= tab[i][0],
                    a1= tab[i][1],
                    a2 = tab[i][2]
                });
        }

        this.lv.View = gridView;
    }

为了动态生成这个:

<ListView  x:Name="lv" HorizontalAlignment="Left" Height="360" Margin="305,192,0,0" VerticalAlignment="Top" Width="607" SelectionMode="Extended"  >

        <ListView.View >
            <GridView  AllowsColumnReorder="true">
                <GridViewColumn DisplayMemberBinding="{Binding sn}" >
                    <GridViewColumnHeader>
                        <GridViewColumnHeader.ContextMenu>
                            <ContextMenu>
                                <MenuItem Header="Asc"/>
                                <MenuItem Header="Desc" />
                            </ContextMenu>
                        </GridViewColumnHeader.ContextMenu>
                    </GridViewColumnHeader>
                </GridViewColumn>

                <GridViewColumn DisplayMemberBinding="{Binding a1}">
                    <GridViewColumnHeader >
                        <GridViewColumnHeader.ContextMenu>
                            <ContextMenu >
                                <MenuItem Header="Asc"  />
                                <MenuItem Header="Desc" />
                            </ContextMenu>
                        </GridViewColumnHeader.ContextMenu>
                    </GridViewColumnHeader>
                </GridViewColumn>

                <GridViewColumn DisplayMemberBinding="{Binding a2}">
                    <GridViewColumnHeader >
                        <GridViewColumnHeader.ContextMenu>
                            <ContextMenu >
                                <MenuItem Header="Asc" />
                                <MenuItem Header="Desc"/>
                            </ContextMenu>
                        </GridViewColumnHeader.ContextMenu>
                    </GridViewColumnHeader>
                </GridViewColumn>
            </GridView>
        </ListView.View>
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Vertical"/>
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
    </ListView>

问题是,当我生成项目时,我看不到 ContextMenu(以及 MenuItems)

我已经在另一个项目中使用了这种代码,但是在这里,它不起作用...... 我猜后面的代码是最后生成的代码,所以如果您能解释一下如何以编程方式创建 ContextMenu。可能会很好。

你能帮我吗?

谢谢!

c# wpf listview contextmenu
2个回答
2
投票

这是

A2
列的示例。其他人也遵循相同的方法。

// Remove:
//gvc3.Header = "A2";
// Replace by:
gvc3.Header = new GridViewColumnHeader()
{
    Content = "A2",
    ContextMenu = new ContextMenu()
    {
        Items =
        {
            new MenuItem() { Header="Asc"},
            new MenuItem() { Header="Desc"}
        }
    }
};

为了实现实际排序并可以按多列排序,可以使用以下辅助函数创建菜单项:

private MenuItem CreateAscendingSortMenuItem(string prop)
{
    var result = new MenuItem() { Header = "Asc" };
    result.Click += (s, e) =>
    {
        var toRemove = lv.Items.SortDescriptions.Where(x => x.PropertyName == prop).ToList();
        foreach (var item in toRemove)
        {
            lv.Items.SortDescriptions.Remove(item);
        }
        lv.Items.SortDescriptions.Insert(0, new SortDescription(prop, ListSortDirection.Ascending));
    };
    return result;
}
private MenuItem CreateDescendingSortMenuItem(string prop)
{
    var result = new MenuItem() { Header = "Desc" };
    result.Click += (s, e) =>
    {
        var toRemove = lv.Items.SortDescriptions.Where(x => x.PropertyName == prop).ToList();
        foreach (var item in toRemove)
        {
            lv.Items.SortDescriptions.Remove(item);
        }
        lv.Items.SortDescriptions.Insert(0, new SortDescription(prop, ListSortDirection.Descending));
    };
    return result;
}

然后只需使用辅助函数创建项目,而不是直接调用构造函数。

gvc3.Header = new GridViewColumnHeader()
{
    Content = "A2",
    ContextMenu = new ContextMenu()
    {
        Items =
        {
            CreateAscendingSortMenuItem("a2"),
            CreateDescendingSortMenuItem("a2")
        }
    }
};

0
投票

谢谢 grek40!

除了您的答案(如何为 MenuItem 创建事件):

// Remove:
//gvc3.Header = "A2";
// Replace by:
MenuItem item1 = New MenuItem();
item1.Header = "Desc";

//Event
item1+= new RoutedEventHandler(this.someFunction_click);

gvc3.Header = new GridViewColumnHeader()
{
    Content = "A2",
ContextMenu = new ContextMenu()
{
    Items =
    {
        new MenuItem() { Header="Asc"},
        item1
    }
}
};

//Function launched by the event
private void someFunction_Click(object sender, System.EventArgs e)
{
    //do something
}

谢谢大家的帮助!!!

© www.soinside.com 2019 - 2024. All rights reserved.