我有一个ListBox(绑定到一个可观察的集合),每个项目都显示为tile。 ListBoxItems
的样式在我的App.xaml中定义,并与属性ItemContainerStyle链接。我想为每个tile显示一个ContextMenu,其中一个MenuItem为“Pin to Start”。这确实有效,但是我仍然启用了MenuItem,尽管我将IsEnabled属性绑定到返回false的ViewModel属性。如果我在我的XAML代码中将IsEnabled属性设置为false(没有绑定),我就会遇到与启用MenuItem相同的问题。
App.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>
VieModel属性:
public bool IsNotPinned
{
get
{
//return !TileManager.IsTileAvailable(this);
return false;
}
}
public ICommand PinToStartCommand { get; private set; }
private void PinToStart(Item item)
{
//...
}