.net - .MAUI : How to add context menu into control by using community-toolkit-markup or C#

标签 .net maui markup-extensions maui-community-toolkit

现在在 .Net7 中,我可以将上下文菜单添加到控件中,如下所示:

Entry
                    x:Name="MyEntry"
                
                    BackgroundColor="AliceBlue"
                    
                    Keyboard="{Binding KeyboardValue, Source={x:Reference Me}}"
                    TextColor="{Binding TextColor, Source={x:Reference Me}}"
                
                    VerticalTextAlignment="Center" 
                    HorizontalOptions ="Fill"
                    HorizontalTextAlignment="{Binding HorizontalTextAlignmentOption, Source={x:Reference Me}}"
            
                    IsEnabled="{Binding IsEnable, Source={x:Reference Me}}"
                    IsReadOnly="{Binding IsReadOnly, Source={x:Reference Me}}"
            
                    Text="{Binding TextValue, Source={x:Reference Me}}"
            
                    Placeholder="{Binding Placeholder, Source={x:Reference Me}}"
                    ToolTipProperties.Text="{Binding TooltipValue, Source={x:Reference Me}}"
                >

                    <FlyoutBase.ContextFlyout>
                        <MenuFlyout x:Name="MyContextMenus">
                            <MenuFlyoutItem Text="Menu1"/>
                            <MenuFlyoutItem Text="Menu2"/>
                        </MenuFlyout>
                    </FlyoutBase.ContextFlyout>
                </Entry>

但是我们需要使用 C# 标记来使用条件(在某些情况下)来显示控件的上下文菜单 - 而不是像上面那样使用 XAML。我们怎样才能做到呢?

最佳答案

您可以尝试使用 C# 代码添加它,例如:

MenuFlyout menuElements = new MenuFlyout();
MenuFlyoutItem item1 = new MenuFlyoutItem() { Text = "menu1" };
MenuFlyoutItem item2 = new MenuFlyoutItem() { Text = "menu2" };
menuElements.Add(item1);
menuElements.Add(item2);
FlyoutBase.SetContextFlyout(MyEntry, menuElements);

关于.net - .MAUI : How to add context menu into control by using community-toolkit-markup or C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74740081/

相关文章:

.net - 在缓存外生成原生图像

c# - C# 中的基本构造函数 - 首先调用哪个?

c# - .NET MAUI VISUAL STUDIOS 2022 如何让状态栏全透明

c# - 标记扩展 : Converting a simple property to DependencyProperty

c# - TryParse 为可空类型

c# - WPF ListView 对列单击进行排序

c# - 波莉 - 'Cannot access a closed Stream'

c# - 如何添加工具栏项 .net maui?

c# - WPF - 通过相对源绑定(bind)为自定义标记扩展提供设计时值(value)

xaml - 将 IMarkupExtension 与 StringFormat 一起使用