HamburgerMenuItem IsEnabled property cannot be updated on runtime
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am having a problem updating the IsEnabled property from HamburgerMenuItem.
First, I set the value of IsEnabledProperty to be false, and then during runtime, I am trying to change it to true, but debugger keeps saying that the value is false. And, of course the UI also still shows that it is disabled. Below is the code:
View:
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
<UserControl.Resources>
<DataTemplate x:Key="MenuItemTemplate" DataType="{x:Type metro:HamburgerMenuGlyphItem}">
<Grid Height="50">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column=0 Text="{Binding Glyph}"/>
<TextBlock Grid.Column=1 Text="{Binding Label}"/>
</Grid>
</DataTemplate>
</UserControl.Resources>
<controls:x:Name="HamburgerMenuControl"
DisplayMode="CompactInline"
ItemTemplate="{StaticResource MenuItemTemplate}"
ItemsSource="{Binding MenuItems, Mode=OneWay}"/>
<Button Content="Test Button" Command="{Binding TestCommand, Mode=OneTime}"/>
ViewModel:
private HamburgerMenuItemCollection menuItems;
public HamburgerMenuItemCollection MenuItems
{
get {return menuItems;}
set {SetProperty(ref menuItems, value);}
}
public ICommand TestCommand {get; private set;}
public MyVM()
{
MenuItems = new HamburgerMenuItemCollection()
{
new HamburgerMenuGlyphItem()
{
Glyph="1",
Label="1"
};
// Second MenuItem is disabled
new HamburgerMenuGlyphItem()
{
Glyph="2",
Label="2",
IsEnabled=false;
};
};
TestCommand = new DelegateCommand(UpdateIsEnabled);
}
private void UpdateIsEnabled()
{
var menuItem = MenuItems[1];
menuItem.IsEnabled = true; // after this is executed, Debugger shows that IsEnabled is still false;
if (menuItem.IsEnabled != isEnabled)
{
vm.BeginInvoke(() =>
{
vm.IsEnabled = isEnabled; // redo, but it seems like none of these are called
vm.SetValue(HamburgerMenuGlyphItem.IsEnabledProperty, isEnabled);
vm.SetCurrentValue(HamburgerMenuGlyphItem.IsEnabledProperty, isEnabled);
vm.InvalidateProperty(HamburgerMenuGlyphItem.IsEnabledProperty);
}, System.Windows.Threading.DispatcherPriority.Normal);
}
RaisePropertyChanged(nameof(MenuItems));
}
Can you show me what I did wrong and how to update the IsEnabled Dependency Property properly?
wpf xaml mahapps.metro
add a comment |
I am having a problem updating the IsEnabled property from HamburgerMenuItem.
First, I set the value of IsEnabledProperty to be false, and then during runtime, I am trying to change it to true, but debugger keeps saying that the value is false. And, of course the UI also still shows that it is disabled. Below is the code:
View:
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
<UserControl.Resources>
<DataTemplate x:Key="MenuItemTemplate" DataType="{x:Type metro:HamburgerMenuGlyphItem}">
<Grid Height="50">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column=0 Text="{Binding Glyph}"/>
<TextBlock Grid.Column=1 Text="{Binding Label}"/>
</Grid>
</DataTemplate>
</UserControl.Resources>
<controls:x:Name="HamburgerMenuControl"
DisplayMode="CompactInline"
ItemTemplate="{StaticResource MenuItemTemplate}"
ItemsSource="{Binding MenuItems, Mode=OneWay}"/>
<Button Content="Test Button" Command="{Binding TestCommand, Mode=OneTime}"/>
ViewModel:
private HamburgerMenuItemCollection menuItems;
public HamburgerMenuItemCollection MenuItems
{
get {return menuItems;}
set {SetProperty(ref menuItems, value);}
}
public ICommand TestCommand {get; private set;}
public MyVM()
{
MenuItems = new HamburgerMenuItemCollection()
{
new HamburgerMenuGlyphItem()
{
Glyph="1",
Label="1"
};
// Second MenuItem is disabled
new HamburgerMenuGlyphItem()
{
Glyph="2",
Label="2",
IsEnabled=false;
};
};
TestCommand = new DelegateCommand(UpdateIsEnabled);
}
private void UpdateIsEnabled()
{
var menuItem = MenuItems[1];
menuItem.IsEnabled = true; // after this is executed, Debugger shows that IsEnabled is still false;
if (menuItem.IsEnabled != isEnabled)
{
vm.BeginInvoke(() =>
{
vm.IsEnabled = isEnabled; // redo, but it seems like none of these are called
vm.SetValue(HamburgerMenuGlyphItem.IsEnabledProperty, isEnabled);
vm.SetCurrentValue(HamburgerMenuGlyphItem.IsEnabledProperty, isEnabled);
vm.InvalidateProperty(HamburgerMenuGlyphItem.IsEnabledProperty);
}, System.Windows.Threading.DispatcherPriority.Normal);
}
RaisePropertyChanged(nameof(MenuItems));
}
Can you show me what I did wrong and how to update the IsEnabled Dependency Property properly?
wpf xaml mahapps.metro
add a comment |
I am having a problem updating the IsEnabled property from HamburgerMenuItem.
First, I set the value of IsEnabledProperty to be false, and then during runtime, I am trying to change it to true, but debugger keeps saying that the value is false. And, of course the UI also still shows that it is disabled. Below is the code:
View:
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
<UserControl.Resources>
<DataTemplate x:Key="MenuItemTemplate" DataType="{x:Type metro:HamburgerMenuGlyphItem}">
<Grid Height="50">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column=0 Text="{Binding Glyph}"/>
<TextBlock Grid.Column=1 Text="{Binding Label}"/>
</Grid>
</DataTemplate>
</UserControl.Resources>
<controls:x:Name="HamburgerMenuControl"
DisplayMode="CompactInline"
ItemTemplate="{StaticResource MenuItemTemplate}"
ItemsSource="{Binding MenuItems, Mode=OneWay}"/>
<Button Content="Test Button" Command="{Binding TestCommand, Mode=OneTime}"/>
ViewModel:
private HamburgerMenuItemCollection menuItems;
public HamburgerMenuItemCollection MenuItems
{
get {return menuItems;}
set {SetProperty(ref menuItems, value);}
}
public ICommand TestCommand {get; private set;}
public MyVM()
{
MenuItems = new HamburgerMenuItemCollection()
{
new HamburgerMenuGlyphItem()
{
Glyph="1",
Label="1"
};
// Second MenuItem is disabled
new HamburgerMenuGlyphItem()
{
Glyph="2",
Label="2",
IsEnabled=false;
};
};
TestCommand = new DelegateCommand(UpdateIsEnabled);
}
private void UpdateIsEnabled()
{
var menuItem = MenuItems[1];
menuItem.IsEnabled = true; // after this is executed, Debugger shows that IsEnabled is still false;
if (menuItem.IsEnabled != isEnabled)
{
vm.BeginInvoke(() =>
{
vm.IsEnabled = isEnabled; // redo, but it seems like none of these are called
vm.SetValue(HamburgerMenuGlyphItem.IsEnabledProperty, isEnabled);
vm.SetCurrentValue(HamburgerMenuGlyphItem.IsEnabledProperty, isEnabled);
vm.InvalidateProperty(HamburgerMenuGlyphItem.IsEnabledProperty);
}, System.Windows.Threading.DispatcherPriority.Normal);
}
RaisePropertyChanged(nameof(MenuItems));
}
Can you show me what I did wrong and how to update the IsEnabled Dependency Property properly?
wpf xaml mahapps.metro
I am having a problem updating the IsEnabled property from HamburgerMenuItem.
First, I set the value of IsEnabledProperty to be false, and then during runtime, I am trying to change it to true, but debugger keeps saying that the value is false. And, of course the UI also still shows that it is disabled. Below is the code:
View:
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
<UserControl.Resources>
<DataTemplate x:Key="MenuItemTemplate" DataType="{x:Type metro:HamburgerMenuGlyphItem}">
<Grid Height="50">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column=0 Text="{Binding Glyph}"/>
<TextBlock Grid.Column=1 Text="{Binding Label}"/>
</Grid>
</DataTemplate>
</UserControl.Resources>
<controls:x:Name="HamburgerMenuControl"
DisplayMode="CompactInline"
ItemTemplate="{StaticResource MenuItemTemplate}"
ItemsSource="{Binding MenuItems, Mode=OneWay}"/>
<Button Content="Test Button" Command="{Binding TestCommand, Mode=OneTime}"/>
ViewModel:
private HamburgerMenuItemCollection menuItems;
public HamburgerMenuItemCollection MenuItems
{
get {return menuItems;}
set {SetProperty(ref menuItems, value);}
}
public ICommand TestCommand {get; private set;}
public MyVM()
{
MenuItems = new HamburgerMenuItemCollection()
{
new HamburgerMenuGlyphItem()
{
Glyph="1",
Label="1"
};
// Second MenuItem is disabled
new HamburgerMenuGlyphItem()
{
Glyph="2",
Label="2",
IsEnabled=false;
};
};
TestCommand = new DelegateCommand(UpdateIsEnabled);
}
private void UpdateIsEnabled()
{
var menuItem = MenuItems[1];
menuItem.IsEnabled = true; // after this is executed, Debugger shows that IsEnabled is still false;
if (menuItem.IsEnabled != isEnabled)
{
vm.BeginInvoke(() =>
{
vm.IsEnabled = isEnabled; // redo, but it seems like none of these are called
vm.SetValue(HamburgerMenuGlyphItem.IsEnabledProperty, isEnabled);
vm.SetCurrentValue(HamburgerMenuGlyphItem.IsEnabledProperty, isEnabled);
vm.InvalidateProperty(HamburgerMenuGlyphItem.IsEnabledProperty);
}, System.Windows.Threading.DispatcherPriority.Normal);
}
RaisePropertyChanged(nameof(MenuItems));
}
Can you show me what I did wrong and how to update the IsEnabled Dependency Property properly?
wpf xaml mahapps.metro
wpf xaml mahapps.metro
edited Jan 8 at 1:31
kurakura88
asked Jan 3 at 14:43
kurakura88kurakura88
1,6421417
1,6421417
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
To enable the HamburgerMenuItem
you should sets its Command
property to a command whose CanExecute
method returns true
.
You cannot just set the IsEnabled
property to true
. If you take a look at how the HamburgerMenuItem class is implemented on GitHub, you'll see that there is a CoerceValueCallback
that returns the value of the private CanExecute
method:
private static object IsEnabledCoerceValueCallback(DependencyObject d, object value)
{
if (!(bool)value)
{
return false;
}
return ((HamburgerMenuItem)d).CanExecute;
}
Sounds like IsEnabledProperty is really useless, because it cannot be changed once value is set. (Or maybe because I am not binding it to some property). In the meantime, I will use the ICommand's CanExecute as you suggested then.
– kurakura88
Jan 4 at 0:07
It doesn't matter whether you bind it to some property. You need to use the Command property to enable/disable the item.
– mm8
Jan 4 at 8:38
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54024522%2fhamburgermenuitem-isenabled-property-cannot-be-updated-on-runtime%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
To enable the HamburgerMenuItem
you should sets its Command
property to a command whose CanExecute
method returns true
.
You cannot just set the IsEnabled
property to true
. If you take a look at how the HamburgerMenuItem class is implemented on GitHub, you'll see that there is a CoerceValueCallback
that returns the value of the private CanExecute
method:
private static object IsEnabledCoerceValueCallback(DependencyObject d, object value)
{
if (!(bool)value)
{
return false;
}
return ((HamburgerMenuItem)d).CanExecute;
}
Sounds like IsEnabledProperty is really useless, because it cannot be changed once value is set. (Or maybe because I am not binding it to some property). In the meantime, I will use the ICommand's CanExecute as you suggested then.
– kurakura88
Jan 4 at 0:07
It doesn't matter whether you bind it to some property. You need to use the Command property to enable/disable the item.
– mm8
Jan 4 at 8:38
add a comment |
To enable the HamburgerMenuItem
you should sets its Command
property to a command whose CanExecute
method returns true
.
You cannot just set the IsEnabled
property to true
. If you take a look at how the HamburgerMenuItem class is implemented on GitHub, you'll see that there is a CoerceValueCallback
that returns the value of the private CanExecute
method:
private static object IsEnabledCoerceValueCallback(DependencyObject d, object value)
{
if (!(bool)value)
{
return false;
}
return ((HamburgerMenuItem)d).CanExecute;
}
Sounds like IsEnabledProperty is really useless, because it cannot be changed once value is set. (Or maybe because I am not binding it to some property). In the meantime, I will use the ICommand's CanExecute as you suggested then.
– kurakura88
Jan 4 at 0:07
It doesn't matter whether you bind it to some property. You need to use the Command property to enable/disable the item.
– mm8
Jan 4 at 8:38
add a comment |
To enable the HamburgerMenuItem
you should sets its Command
property to a command whose CanExecute
method returns true
.
You cannot just set the IsEnabled
property to true
. If you take a look at how the HamburgerMenuItem class is implemented on GitHub, you'll see that there is a CoerceValueCallback
that returns the value of the private CanExecute
method:
private static object IsEnabledCoerceValueCallback(DependencyObject d, object value)
{
if (!(bool)value)
{
return false;
}
return ((HamburgerMenuItem)d).CanExecute;
}
To enable the HamburgerMenuItem
you should sets its Command
property to a command whose CanExecute
method returns true
.
You cannot just set the IsEnabled
property to true
. If you take a look at how the HamburgerMenuItem class is implemented on GitHub, you'll see that there is a CoerceValueCallback
that returns the value of the private CanExecute
method:
private static object IsEnabledCoerceValueCallback(DependencyObject d, object value)
{
if (!(bool)value)
{
return false;
}
return ((HamburgerMenuItem)d).CanExecute;
}
answered Jan 3 at 15:37


mm8mm8
89.2k81934
89.2k81934
Sounds like IsEnabledProperty is really useless, because it cannot be changed once value is set. (Or maybe because I am not binding it to some property). In the meantime, I will use the ICommand's CanExecute as you suggested then.
– kurakura88
Jan 4 at 0:07
It doesn't matter whether you bind it to some property. You need to use the Command property to enable/disable the item.
– mm8
Jan 4 at 8:38
add a comment |
Sounds like IsEnabledProperty is really useless, because it cannot be changed once value is set. (Or maybe because I am not binding it to some property). In the meantime, I will use the ICommand's CanExecute as you suggested then.
– kurakura88
Jan 4 at 0:07
It doesn't matter whether you bind it to some property. You need to use the Command property to enable/disable the item.
– mm8
Jan 4 at 8:38
Sounds like IsEnabledProperty is really useless, because it cannot be changed once value is set. (Or maybe because I am not binding it to some property). In the meantime, I will use the ICommand's CanExecute as you suggested then.
– kurakura88
Jan 4 at 0:07
Sounds like IsEnabledProperty is really useless, because it cannot be changed once value is set. (Or maybe because I am not binding it to some property). In the meantime, I will use the ICommand's CanExecute as you suggested then.
– kurakura88
Jan 4 at 0:07
It doesn't matter whether you bind it to some property. You need to use the Command property to enable/disable the item.
– mm8
Jan 4 at 8:38
It doesn't matter whether you bind it to some property. You need to use the Command property to enable/disable the item.
– mm8
Jan 4 at 8:38
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54024522%2fhamburgermenuitem-isenabled-property-cannot-be-updated-on-runtime%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown