WPF MenuItem:混合数据绑定项和静态内容

时间:2010-06-28 14:05:29

标签: wpf data-binding xaml mvvm mru

我想构建一个具有以下结构的MRU菜单:

File
+=>Recent Files
   +=> Doc1.txt
   +=> Doc2.txt
   -separator-
   +=> Clear entries

这是MVVM,我的mru列表是视图模型的数据绑定。因为我想添加分隔符,并且我不想在项目列表中插入分隔符和清除条目操作,我当前正在为我的菜单使用itemscontrol容器,但是我遇到了可怕的填充问题。您是否有任何解决方案可以让我只添加MenuItem个实例?

这是XAML:

<!-- MRU list -->
<MenuItem Header="_Recent Files" >

    <ItemsControl ItemsSource="{Binding MostRecentlyUsed.Entries,Mode=OneWay}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <MenuItem Header="{Binding ShortName}" ToolTip="{Binding FileName}" Command="{Binding OpenCommand}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    <Separator />
    <MenuItem Header="_Clean Entries" Command="{Binding MostRecentlyUsed.CleanCommand}" />
</MenuItem>
<Separator />

干杯, 弗洛里安

1 个答案:

答案 0 :(得分:8)

将DataTemplate替换为ItemContainerStyle,如下所示:

<MenuItem Header="_Recent Files" ItemsSource="{Binding Commands,Mode=OneWay}">
  <MenuItem.ItemContainerStyle>
    <Style TargetType="{x:Type MenuItem}">
       <Setter Property="Header" Value="{Binding Path=ShortName}" />
       <Setter Property="ToolTip" Value="{Binding Path=FileName}" />
       <Setter Property="Command" Value="{Binding Path=OpenCommand}" />
       <Setter Property="CommandParameter" Value="{Binding Path=OpenParameter}" />
       <Style.Triggers>
         <DataTrigger Binding="{Binding Path=IsSeparator}" Value="true">
           <Setter Property="MenuItem.Template">
             <Setter.Value>
               <ControlTemplate TargetType="{x:Type MenuItem}">
                 <Separator Style="{DynamicResource {x:Static MenuItem.SeparatorStyleKey}}"/>
               </ControlTemplate>
             </Setter.Value>
           </Setter>
         </DataTrigger>
       </Style.Triggers>
     </Style>
   </MenuItem.ItemContainerStyle>
 </MenuItem>

这是我绑定的代码,类似于你的代码(但不同):

public class MainViewModel : ViewModelBase
{
  public MainViewModel()
  {
     GoCommand = new DelegateCommand<object>(OnGoCommand, CanGoCommand);
     LoadCommands();
  }

  private List<MyCommand> _commands = new List<MyCommand>();
  public List<MyCommand> Commands
  {
     get { return _commands; }
  }

  private void LoadCommands()
  {
     MyCommand c1 = new MyCommand { OpenCommand = GoCommand, OpenParameter = "1", ShortName = "Doc1", FileName = "Doc1.txt", IsSeparator = false };
     MyCommand c2 = new MyCommand { OpenCommand = GoCommand, OpenParameter = "2", ShortName = "Doc2", FileName = "Doc1.txt", IsSeparator = false };
     MyCommand c3 = new MyCommand { OpenCommand = GoCommand, OpenParameter = "4", ShortName = "", FileName = "", IsSeparator = true };
     MyCommand c4 = new MyCommand { OpenCommand = GoCommand, OpenParameter = "5", ShortName = "_Clean Entries", FileName = "Clean Entries", IsSeparator = false };
     _commands.Add(c1);
     _commands.Add(c2);
     _commands.Add(c3);
     _commands.Add(c4);
  }

  public ICommand GoCommand { get; private set; }
  private void OnGoCommand(object obj)
  {
  }

  private bool CanGoCommand(object obj)
  {
     return true;
  }
}


public class MyCommand
{
  public ICommand OpenCommand { get; set; }
  public string ShortName { get; set; }
  public string FileName { get; set; }
  public string OpenParameter { get; set; }
  public bool IsSeparator { get; set; }
}