设置样式为null后,为什么忽略是没用的?

时间:2017-06-16 06:21:27

标签: c# wpf data-binding

我有一个MenuItem,项目总是使用相同的样式,唯一一个项目使用不同的样式。

所以,我将两种风格设置为相同的目标类型。只需看看我的xaml代码:

<Menu>
  <Menu.Resources>
      <Style TargetType="{x:Type MenuItem}">
          <Setter Property="Command" Value="{Binding ActionCommand}"/>
          <Setter Property="CommandParameter" Value="{Binding RelativeSource={RelativeSource Self}, Path=Header}"/>
      </Style>
      <!--Hierarchical MenuItmes-->
      <Style x:Key="MenuItemsCommand" TargetType="{x:Type MenuItem}">
          <Setter Property="Command" Value="{Binding Command}" />
      </Style>
      <HierarchicalDataTemplate x:Key="ItemsTemplate"
                        DataType="{x:Type vm:MenuItemsViewModel}" 
                        ItemsSource="{Binding MenuItems}">
          <TextBlock Text="{Binding Header}"/>
      </HierarchicalDataTemplate>
  </Menu.Resources>
  <MenuItem Header="Files">
      <MenuItem Header="{Binding Open}" InputGestureText="Ctrl+O"/>

      <!--ignore default style-->
      <MenuItem Header="Open Recent file"
            Style="{x:Null}"  
            ItemsSource="{Binding RecentFiles}"
            ItemContainerStyle="{StaticResource MenuItemsCommand}"
            ItemTemplate="{StaticResource ItemsTemplate}">
      </MenuItem>

      <MenuItem Header="_Save" CommandParameter="{Binding Save}" InputGestureText="Ctrl+S"/>
  </MenuItem>
</Menu>

MenuItems始终绑定ActionCommand。但是有人绑定了Command。

所以我想忽略原始样式,设置Style="{x:Null}"。但是当我打开这个MeunItems时,我遇到了绑定错误。

System.Windows.Data Error: 40 : BindingExpression path error: 'ActionCommand' property not found on 'object' ''OpenFileList' (HashCode=27335113)'. BindingExpression:Path=ActionCommand; DataItem='OpenFileList' (HashCode=27335113); target element is 'MenuItem' (Name=''); target property is 'Command' (type 'ICommand')

忽视是有效的吗?我不知道该做些什么。

1 个答案:

答案 0 :(得分:0)

为什么你在这里使用隐式Style?您是否只能将StyleOpen项的Save属性设置为绑定到Command属性的样式?:

<Menu>
    <Menu.Resources>
        <Style x:Key="style1" TargetType="{x:Type MenuItem}">
            <Setter Property="Command" Value="{Binding ActionCommand}"/>
            <Setter Property="CommandParameter" Value="{Binding RelativeSource={RelativeSource Self}, Path=Header}"/>
        </Style>

        <Style x:Key="MenuItemsCommand" TargetType="{x:Type MenuItem}">
            <Setter Property="Command" Value="{Binding Command}" />
        </Style>

        <HierarchicalDataTemplate x:Key="ItemsTemplate"
                        DataType="{x:Type vm:MenuItemsViewModel}" 
                        ItemsSource="{Binding MenuItems}">
            <TextBlock Text="{Binding Header}"/>
        </HierarchicalDataTemplate>
    </Menu.Resources>
    <MenuItem Header="Files">
        <MenuItem Header="{Binding Open}" InputGestureText="Ctrl+O" Style="{StaticResource style1}"/>

        <!--ignore default style-->
        <MenuItem Header="Open Recent file"
                    ItemsSource="{Binding RecentFiles}"
                    ItemContainerStyle="{StaticResource MenuItemsCommand}"
                    ItemTemplate="{StaticResource ItemsTemplate}">
        </MenuItem>

        <MenuItem Header="_Save" CommandParameter="{Binding Save}" InputGestureText="Ctrl+S" Style="{StaticResource style1}"/>
    </MenuItem>
</Menu>

或者您只是想将“打开最近的文件”的Command属性设置为null?:

<MenuItem Header="Open Recent file"
          Command="{x:Null}"
          ItemsSource="{Binding RecentFiles}"
          ItemContainerStyle="{StaticResource MenuItemsCommand}"
          ItemTemplate="{StaticResource ItemsTemplate}">

在提问时,您应该始终创建问题Minimal, Complete, and Verifiable example。在这种情况下,XAML标记不足以重现您的问题。