2017-02-14 9 views
0

我知道,我真的應該開始讀一本有關XAML和WPF,因爲我覺得我所有的問題在這裏屬於缺乏有關數據綁定的理解(我用的WinForms年):DataGridRow和DataGridRow的ContextMenuItems之間的數據綁定如何在封裝的Viewmodel中工作?

我的應用程序包括一個TreeView和一個DataGrid。

在TreeView中,我爲每個ParentNode,ChildNode和GrandChildNode添加了ViewModels。

我用過Josh Smith的樣品found here

要短,他/ I使用

<HierarchicalDataTemplate 
        DataType="{x:Type local:ViewModel.TreeViewChildNodeViewModel}" 
        ItemsSource="{Binding Children}"> 
</HierarchicalDataTemplate.Resources> 

到ChildNode到ChildNodeViewModel和到相應的模型結合。

我不是說 - 在TreeViewChildNodeViewModel構造:

ContextMenuItems = new List<MenuItem>(); 
ContextMenuItems.Add(new MenuItem() { 
          Header = "Click", 
          Command = _cmdDoSmth 
          ToolTip = new ToolTip() { Content = "blabla" } 
            } 
); 

在暴露於觀與這個屬性:

private readonly List<MenuItem> ContextMenuItems; 
public List<MenuItem> ContextMenu { 
    get { return ContextMenuItems; } 
} 

需要注意的是,我有多個構造函數。我將不同的ContextMenuItems添加到ContextMenu列表,具體取決於我希望ViewModel使用的模型。 「根」ChildNode包括:

<TextBlock 
    Text="{Binding ChildNodeDisplayItem}"> 
    <TextBlock.ContextMenu> 
     <ContextMenu 
      ItemsSource="{Binding ContextMenu}"></ContextMenu> 
    </TextBlock.ContextMenu> 
</TextBlock> 

它的工作原理應該如此。現在我的問題開始試圖做一些類似的數據網格。

我需要實現的是:

我想顯示在DataGrid行。每行都有自己的ViewModel,並帶有一個公開的ContextMenuItem列表(以及當然的模型)。我希望能夠根據所選的視圖模型來定義每個contextmenuitem的計數,標題和命令。

我做了什麼至今:

在我MainWindow.xaml:

<Controls:MetroWindow.Resources> 
    <ContextMenu x:Key="DataRowContextMenu" ItemsSource="{Binding Path=ActionReactionDataGridViewModel/ContextMenu, RelativeSource={RelativeSource AncestorType=DataGrid, Mode=FindAncestor}}"/> 
</Controls:MetroWindow.Resources> 

<DataGrid 
    AutoGenerateColumns="True" 
    AutoGeneratingColumn="OnAutoGeneratingColumn" 
    HorizontalAlignment="Stretch" 
    VerticalAlignment="Stretch" 
    BorderThickness="1,1,1,1" 
    Margin="0,0,0,0" 
    ItemsSource="{Binding Path=ActionReactionDataGridViewModel/DataGridSource}" 
    SelectedItem="{Binding Path=ActionReactionDataGridViewModel/SelectedDataGridItem}" 
    BorderBrush="#FF020202"> 
    <DataGrid.RowStyle> 
     <Style TargetType="{x:Type DataGridRow}"> 
      <Setter Property="ContextMenu" Value="{StaticResource RowMenu}" />    </Style> 
    <DataGrid.RowStyle> 
</DataGrid> 

在我MainWindowViewModel:

public MainWindowViewModel() // Constructor 
    { 
     actionReactionDataGrid = new ObservableCollection<ActionReactionDataGridViewModel>(); 
     actionReactionDataGrid.Add(new ActionReactionDataGridViewModel()); 

    } 

    private ObservableCollection<ActionReactionDataGridViewModel> actionReactionDataGrid; 
    public ObservableCollection<ActionReactionDataGridViewModel> ActionReactionDataGridViewModel 
    { 
     get { return actionReactionDataGrid; } 
    } 

我ActionReactionDataGridViewModel是在這裏:

public class ActionReactionDataGridViewModel : ViewModelBase 
{ 
    private readonly List<MenuItem> ContextMenuItems; 

    public ActionReactionDataGridViewModel() 
    { 

     ContextMenuItems = new List<MenuItem>(); 

     ContextMenuItems.Add(new MenuItem() 
          { 
           Header = "blubb" 
          }); 

     dataGridSource = new ObservableCollection<ActionReactionDataGridModel>(); 
     dataGridSource.Add(new ActionReactionDataGridModel("Status","Eventname","Eventtyp","ReaktionName","ReaktionTyp")); 
    } 

    public List<MenuItem> ContextMenu { 
     get { return ContextMenuItems; } 
    } 

    private ActionReactionDataGridModel selectedDataGridItem; 
    public ActionReactionDataGridModel SelectedDataGridItem { 
     get { return selectedDataGridItem; } 
     set {selectedDataGridItem = value; RaisePropertyChanged("SelectedDataGridItem"); } 
    } 

    private ObservableCollection<ActionReactionDataGridModel> dataGridSource; 
    public ObservableCollection<ActionReactionDataGridModel> DataGridSource { 
     get { return dataGridSource; } 
     set { dataGridSource = value; RaisePropertyChanged("DataGridSource"); } 
    } 


} 

一世 認爲發佈模型的內容不是必需的,因爲它只包含列標題和一些示例字符串。我認爲我缺少的是在MainWindow.xaml的視圖中告訴DataGrid控件將itemssource綁定到「DataGridSource」而不是「ActionReactionDataGridViewModel」的知識。

我在SO上發現了其他關於將上下文菜單添加到datagridrow的帖子。我所缺少的是將計數,文本和命令綁定到每個視圖模型的能力。

任何幫助將不勝感激。

謝謝。

// EDIT 1

確定。瞭解如何從視圖模型集合中傳遞視圖模型的屬性很容易。

我加入

ItemsSource="{Binding Path=ActionReactionDataGridViewModel/DataGridSource} 

解釋是here

現在我 「只是」 需要弄清楚如何將文本菜單項添加到每個視圖模型...

回答

0
<DataGrid.ContextMenu> 
       <ContextMenu> 
        <MenuItem Header="HeaderName"> 
        </MenuItem> 
       </ContextMenu> 
      </DataGrid.ContextMenu> 

內部菜單物品你可以寫你的控制。

+0

這是否導致contextmenu在控件中的任何地方顯示?我希望它能得到「ActionReactionDataGridViewModel.ContextMenu」和整個行。 – c3rebro

+0

任何位於DataGrid裏面的地方 –

相關問題