0

我有一个 ListBox(绑定到一个可观察的集合),每个项目都显示为图块。的样式ListBoxItems在我的 App.xaml 中定义,并与属性 ItemContainerStyle 链接。我想为每个磁贴显示一个 ContextMenu,其中包含一个 MenuItem“固定到开始”。这确实有效,但是尽管我将 IsEnabled 属性绑定到返回 false 的 ViewModel 属性,但仍然启用了 MenuItem。如果我在我的 XAML 代码中将 IsEnabled 属性设置为 false(没有绑定),我会遇到同样的问题,即 MenuItem 已启用。

应用程序.xaml

<Style x:Key="TileListBoxItemStyle" TargetType="ListBoxItem">
    <Setter Property="Padding" Value="0"/>
    <Setter Property="Margin" Value="12,12,0,0"/>
    <Setter Property="Background" Value="{StaticResource PhoneAccentBrush}"/>
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="Width" Value="210"/>
    <Setter Property="Height" Value="210"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListBoxItem">
                <Border x:Name="LayoutRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}">
                    <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    <toolkit:ContextMenuService.ContextMenu>
                        <toolkit:ContextMenu>
                            <toolkit:MenuItem 
                                Header="Pin to Start"
                                IsEnabled="{Binding IsNotPinned}"
                                Command="{Binding PinToStartCommand}" 
                                CommandParameter="{Binding}"/>
                        </toolkit:ContextMenu>
                    </toolkit:ContextMenuService.ContextMenu>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

视图模型属性:

public bool IsNotPinned
{
    get
    {
        //return !TileManager.IsTileAvailable(this);
        return false;
    }
}

public ICommand PinToStartCommand { get; private set; }

private void PinToStart(Item item)
{
    //...
}
4

0 回答 0