【发布时间】:2016-06-24 09:12:45
【问题描述】:
我正在处理的应用程序使用 DataGrid 向用户呈现条目,并且这些条目被分组。分组不依赖于每个条目的单个属性,单个条目可以在多个组中。用户可以随意创建组并向这些组添加条目。
我们希望用户能够直接从此视图编辑条目和组。要删除组,我们希望用户能够右键单击组并从上下文菜单中选择“删除组”。
我已经能够为 GroupItem 的 Expander 提供一个上下文菜单,但不知道如何将 Command 或 CommandParameter 绑定到 ViewModel。
我如何实现我所寻求的结果?我很欣赏这可能需要将上下文菜单“移动”到控件的不同部分,但我们希望为组标题的条目提供不同的上下文菜单。这是我们在实时代码中实现的,但在下面的示例中没有体现。
这是一个简化的示例来表示我们正在努力实现的目标。
XAML:
<Window x:Class="DataGridHeaderTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<CollectionViewSource x:Key="GroupedEntriesSource" Source="{Binding Entries}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Key"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
<Style x:Key="GroupContainerStyle" TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True" Background="#414040">
<Expander.ContextMenu>
<ContextMenu>
<!-- How do I bind Command and CommandParameter? -->
<MenuItem Header="Delete group" Command="{Binding DeleteGroupCommand}" CommandParameter="{Binding}" />
</ContextMenu>
</Expander.ContextMenu>
<Expander.Header>
<Grid>
<TextBlock Text="{Binding Path=Items[0].Key.Name}"/>
</Grid>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<DataGrid ItemsSource="{Binding Source={StaticResource GroupedEntriesSource}}" AutoGenerateColumns="False">
<DataGrid.GroupStyle>
<GroupStyle ContainerStyle="{StaticResource GroupContainerStyle}">
<GroupStyle.Panel>
<ItemsPanelTemplate>
<DataGridRowsPresenter/>
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</DataGrid.GroupStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Value.Name, Mode=OneWay}"/>
<DataGridTextColumn Header="Data" Binding="{Binding Value.Val, UpdateSourceTrigger=LostFocus}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
后面的代码:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;
namespace DataGridHeaderTest
{
public partial class MainWindow : Window
{
public MainWindow()
{
CreateData();
DeleteGroupCommand = new TestCommand(DeleteGroup);
DataContext = this;
InitializeComponent();
}
void CreateData()
{
Entries = new ObservableCollection<KeyValuePair<Group, Entry>>();
Group group1 = new Group() { Name = "Group1" };
Group group2 = new Group() { Name = "Group2" };
Entry entry1 = new Entry() { Name = "Entry1", Val = "Val1" };
Entry entry2 = new Entry() { Name = "Entry2", Val = "Val2" };
Entry entry3 = new Entry() { Name = "Entry3", Val = "Val3" };
Entries.Add(new KeyValuePair<Group, Entry>(group1, entry1));
Entries.Add(new KeyValuePair<Group, Entry>(group1, entry3));
Entries.Add(new KeyValuePair<Group, Entry>(group2, entry2));
Entries.Add(new KeyValuePair<Group, Entry>(group2, entry3));
}
void DeleteGroup(object group)
{
// I want to run this when "Delete group" is selected from the context menu of the Group Expander.
// I want the Group object associated with the Group Expander passed as the parameter
}
public ObservableCollection<KeyValuePair<Group, Entry>> Entries { get; set; }
public ICommand DeleteGroupCommand { get; set; }
public class Group
{
public string Name { get; set; }
}
public class Entry
{
public string Name { get; set; }
public string Val { get; set; }
}
public class TestCommand : ICommand
{
public delegate void ICommandOnExecute(object parameter);
private ICommandOnExecute _execute;
public TestCommand(ICommandOnExecute onExecuteMethod)
{
_execute = onExecuteMethod;
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
_execute.Invoke(parameter);
}
}
}
}
【问题讨论】:
-
同时尝试发布更清晰的图片。
标签: c# wpf mvvm data-binding