简体   繁体   English

在WPF中禁用上下文菜单按钮

[英]Disable a context menu button in WPF

I created a button that looks like: 我创建了一个如下所示的按钮:

在此处输入图片说明

Here is the xaml: 这是xaml:

<Button x:Name="InstallButtonContainer" Style="{StaticResource ResourceKey=StyleAppButton}" Grid.Column="3" >
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="15"/>
        </Grid.ColumnDefinitions>
        <Button x:Name="InstallButton" Content="Install" Grid.Column="0"
            Style="{StaticResource ResourceKey= StyleDropDownButton}"
            ToolTip="{x:Static local:ToolTipStrings.INSTALLBUTTONTOOLTIP}" Click="InstallButton_Click"
            ToolTipService.ShowDuration="2000" 
            Margin="-20,-2,-4.5,-2" Grid.ColumnSpan="2" Width="51" FontFamily="Calibri"  />
        <Button x:Name="DropdownButton" Grid.Column="1"  Margin="18,-2,-20,-2" 
             Width="14" Click="load_install_dropdown" Style="{StaticResource ResourceKey= StyleDropDownButton}">
            <Button.ContextMenu>
                <ContextMenu x:Name="ButtonContextMenu">
                    <MenuItem Header="Install" Click="BaseReleaseInstallContextMenuClick" x:Name="MultiInstallBtn">
                        <MenuItem.Icon>
                            <Image Width="12" Height="12">
                                <Image.Source>
                                    <ImageSource>Resources/install.ico</ImageSource>
                                </Image.Source>
                            </Image>
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Header="Silent Install" Click="BaseReleaseSilentInstallContextMenuClick" x:Name="MultiInstallSilentBtn">
                        <MenuItem.Icon>
                            <Image Width="12" Height="12">
                                <Image.Source>
                                    <ImageSource>Resources/install.ico</ImageSource>
                                </Image.Source>
                            </Image>
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Header="Download" Click="BaseReleaseMultipleDownloadContextMenuClick">
                        <MenuItem.Icon>
                            <Image Width="12" Height="12">
                                <Image.Source>
                                    <ImageSource>Resources/Down.png</ImageSource>
                                </Image.Source>
                            </Image>
                        </MenuItem.Icon>
                    </MenuItem>
                </ContextMenu>
            </Button.ContextMenu>
            <StackPanel Orientation="Horizontal">
                <Path x:Name="BtnArrow" Margin="-3,-10" VerticalAlignment="Center" Width="8" Height="10" Fill="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" 
                    Stretch="Uniform" HorizontalAlignment="Right" 
                    Data="F1 M 301.14,-189.041L 311.57,-189.041L 306.355,-182.942L 301.14,-189.041 Z "/>
            </StackPanel>
        </Button>
    </Grid>
</Button>

Now based on some condition, I want to disable the Install and Silent Install buttons through code. 现在基于某种条件,我想通过代码禁用“安装”和“静默安装”按钮。

I tried using: 我尝试使用:

if(condition)
{
    MultiInstallBtn.IsEnabled = false;
}

but it does not seems to work. 但它似乎不起作用。 Is there anything wrong in the way I am accessing it? 我的访问方式有什么问题吗?

As long as that code is in the code behind of the control then it should work. 只要该代码位于控件后面的代码中,它就应该起作用。

I notice that there may be some custom styling being applied too. 我注意到可能还会应用一些自定义样式。 Could it be that you are missing styling for the menu item disabled state? 可能是您缺少菜单项禁用状态的样式吗? So the menu item is actually disabled, but visually it doesn't present in a different way? 因此,菜单项实际上是被禁用的,但是在视觉上它不会以其他方式呈现吗?

Check that in the style or control template for the Menu Item that it is reacting to the control's IsEnabled property or that there's a "Disabled" VisualState defined. 检查菜单项的样式或控件模板中是否对控件的IsEnabled属性有反应,或者是否定义了“已禁用” VisualState。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM