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

查看:19
本文介绍了如何使用从 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

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

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"
};

但是我如何添加分隔符?

but how I can add the separator?

推荐答案

我找到了一个解决方法:

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 个转换器,一个用于动态菜单的多重绑定,另一个非常相似,用于新项目和分隔符的参数.现在全部动态!

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天全站免登陆