0

I created a button that looks like:

enter image description here

Here is the 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?

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
The King
  • 833
  • 1
  • 15
  • 41
  • So, although they are disabled you can still click them and they execute? – Manfred Radlwimmer Sep 27 '17 at 12:33
  • 1
    Where are you trying disable menu item? I am unable to reproduce the issue. – Sinatr Sep 27 '17 at 12:35
  • Can you try changing `x:Name="MultiInstallBtn"` to `Name="MultiInstallBtn"`? `Name` setting would mean setting control Name and `x:Name` is mostly used to perform bindings with command in code behind (which you are certainly not doing here!). – praty Sep 27 '17 at 12:36
  • 3
    @praty That is completely incorrect. – Manfred Radlwimmer Sep 27 '17 at 12:49
  • @ManfredRadlwimmer, a reference to my understanding from an old SO answers (para4) here: https://stackoverflow.com/questions/589874/in-wpf-what-are-the-differences-between-the-xname-and-name-attributes#answer-593151. I do not suggest my post to be an answer but just a hit and trial. – praty Sep 27 '17 at 13:22
  • @praty Then you've totally misunderstood how it works. – Manfred Radlwimmer Sep 27 '17 at 13:24
  • @praty btw. it's "Hit-and-miss" or "Trial-and-error" – Manfred Radlwimmer Sep 27 '17 at 13:28
  • You could just use a style and a `Trigger` or `DataTrigger` or even an `EventTrigger` to disable the `ContextMenu`. I have noticed that you already apply a style to your button so add `BasedOn="{StaticResource StyleDropDownButton}"` to your style so you can keep using unified button. – XAMlMAX Sep 27 '17 at 14:28

1 Answers1

0

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.

PuncrOc
  • 1
  • 1