2010-06-28 108 views
6

我想建立一個具有以下結構的MRU菜單:WPF菜單項:將數據綁定項目和靜態內容

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 /> 

乾杯, 弗洛裏安

回答

8

與ItemContainerStyle更換的DataTemplate如下:

<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; } 
} 
相關問題