HamburgerMenuItem IsEnabled属性无法在运行时更新

时间:2019-01-03 14:43:27

标签: wpf xaml mahapps.metro

我在从HamburgerMenuItem更新IsEnabled属性时遇到问题。 首先,我将IsEnabledProperty的值设置为false,然后在运行时将其更改为true,但是调试器会一直说该值为false。而且,当然,UI仍会显示它已被禁用。下面是代码:

查看:

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

您能告诉我我做错了什么以及如何正确更新IsEnabled依赖项属性吗?

1 个答案:

答案 0 :(得分:0)

要启用d:\bat> runas /noprofile /user:user "cmd /V:ON /C whoami&echo \"!CD!\"&echo !CD! Test runas>>\"%CD%\test runas.txt\"&pause" Enter the password for user: Attempting to start cmd /V:ON /C whoami&echo "!CD!"&echo !CD! Test runas>>"d:\bat\test runas.txt"&pause as user "MY-PC\user" ... d:\bat> type "test runas.txt" C:\WINDOWS\system32 Test runas C:\WINDOWS\system32 Test runas ,应将其HamburgerMenuItem属性设置为命令,该命令的Command方法返回CanExecute

您不能仅将true属性设置为IsEnabled。如果看一下how the HamburgerMenuItem class is implemented on GitHub,您会发现有一个true返回私有CoerceValueCallback方法的值:

CanExecute