如何使用由Binding生成的项目和直接创建ContextMenu的菜单 [英] How to create a ContextMenu with items generated from Binding and directly

查看:33
本文介绍了如何使用由Binding生成的项目和直接创建ContextMenu的菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢@thatguy的大力帮助,我能够动态创建菜单,所有详细信息和说明在此处:如何避免在菜单中重复XAML块

Thanks to great @thatguy help I was able to create the menu dynamically, all details and explanation here: How to avoid repeating blocks of XAML in a menu

它工作得很好,但是我的问题是,在列表的末尾,我需要添加一个分隔符和一个Delete项.这是我正在使用的代码:

It works perfectly but my problem is that at the end of the list I need to add a separator and a Delete item. This was the code I was using:

                    <ContextMenu ItemsSource="{Binding MyTypes}" ItemContainerStyle="{StaticResource MyMenuItemStyle}">
                    <Separator HorizontalAlignment="Stretch" Visibility="{Binding MenuSelected.Type, Converter={StaticResource STypeToVisibilityConverter}, ConverterParameter='ANY', Mode=OneWay}" />
                    <MenuItem>
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="Click">
                                <i:InvokeCommandAction Command="{Binding CmdContextMenu}" CommandParameter="DEL" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                        <MenuItem.Header>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <TextBlock
                                    Grid.Column="0"
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Center"
                                    FontFamily="Segoe MDL2 Assets"
                                    Text="&#xE74D;" />
                                <TextBlock
                                    Grid.Column="1"
                                    Margin="{StaticResource XSmallLeftMargin}"
                                    HorizontalAlignment="Left"
                                    VerticalAlignment="Center"
                                    Text="Delete" />
                            </Grid>
                        </MenuItem.Header>
                    </MenuItem>
                </ContextMenu>
            </TreeView.ContextMenu>

但是当我运行它时,我得到一个错误:

but when I run it I get an error:

Message = Items集合在使用ItemsSource之前必须为空.Source = PresentationFramework

Message=Items collection must be empty before using ItemsSource. Source=PresentationFramework

:(

我可以在列表"的列表中添加删除"项.

I could add the Delete item in the list of

public IEnumerable<string> MyTypes { get; } = new List<string>
{
  "PAZ",
  "APP",
  "DEL"
};

但是如何添加分隔符?

推荐答案

我找到了一种解决方法:

I've found a workaround:

                   <ContextMenu>
                    <MenuItem ItemsSource="{Binding MyTypes}" ItemContainerStyle="{StaticResource MyMenuItemStyle}" Visibility="{Binding MenuSelected.Type, Converter={StaticResource STypeToVisibilityConverter}, ConverterParameter='ANY', Mode=OneWay}">
                        <MenuItem.Header>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <TextBlock
                                Grid.Column="0"
                                HorizontalAlignment="Center"
                                VerticalAlignment="Center"
                                FontFamily="Segoe MDL2 Assets"
                                Text="" />
                                <TextBlock
                                Grid.Column="1"
                                Margin="{StaticResource XSmallLeftMargin}"
                                HorizontalAlignment="Left"
                                VerticalAlignment="Center"
                                Text="New" />
                            </Grid>
                        </MenuItem.Header>
                    </MenuItem>
                    <Separator HorizontalAlignment="Stretch" Visibility="{Binding MenuSelected.Type, Converter={StaticResource STypeToVisibilityConverter}, ConverterParameter='ANY', Mode=OneWay}" />
                    <MenuItem>
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="Click">
                                <i:InvokeCommandAction Command="{Binding CmdContextMenu}" CommandParameter="DEL" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                        <MenuItem.Header>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <TextBlock
                                Grid.Column="0"
                                HorizontalAlignment="Center"
                                VerticalAlignment="Center"
                                FontFamily="Segoe MDL2 Assets"
                                Text="&#xE74D;" />
                                <TextBlock
                                Grid.Column="1"
                                Margin="{StaticResource XSmallLeftMargin}"
                                HorizontalAlignment="Left"
                                VerticalAlignment="Center"
                                Text="Delete" />
                            </Grid>
                        </MenuItem.Header>
                    </MenuItem>
                </ContextMenu>

不是我想要的,但是更加干净.我为可见性创建了2个转换器,一个与动态菜单的多重绑定一起使用,一个非常相似,与New项的参数设置和分隔符一起使用,非常相似.现在全部动态!

Is not exactly what I want but is even more clean. I've created 2 converters for the visibility, one that works with the multibinding for the dynamic menu and one, very similar, that works with the paramenter for the New item and the separator. All dynamic now!

这篇关于如何使用由Binding生成的项目和直接创建ContextMenu的菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆