选择子项时,Xceed Datagrid会丢失SelectedItem

时间:2012-09-20 10:47:14

标签: mvvm datagrid selecteditem master-detail xceed

我在WPF MVVM应用程序中有一个Xceed数据网格,它设置为保存主细节记录。选择子行时,我希望ViewModel检测选定的子项。我想这样做最好没有代码隐藏。我编写的代码在单击上下文菜单时对所选项执行操作。这在选择父级时正常工作,但在选择子级时始终返回null。

我已经把我想要实现的非常简化的版本放在一起:

我的XAML是:

    <Window x:Class="MasterDetailSelection.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:xcdg="clr-namespace:Xceed.Wpf.DataGrid;assembly=Xceed.Wpf.DataGrid.v4.2"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <xcdg:DataGridCollectionViewSource x:Key="cvs_parents" Source="{Binding Path=Parents}">
                <xcdg:DataGridCollectionViewSource.DetailDescriptions>
                    <xcdg:PropertyDetailDescription RelationName="Children"
                                                    AutoCreateDetailDescriptions="False">
                    </xcdg:PropertyDetailDescription>
                </xcdg:DataGridCollectionViewSource.DetailDescriptions>
            </xcdg:DataGridCollectionViewSource>
        </Grid.Resources>
        <xcdg:DataGridControl x:Name="ParentGrid"
                              NavigationBehavior="RowOnly"
                              ItemsSource="{Binding Source={StaticResource cvs_parents}}"
                              SelectedItem="{Binding SelectedItem}"
           AutoCreateDetailConfigurations="True"
                              ReadOnly="True">
            <xcdg:DataGridControl.ContextMenu>
                <ContextMenu DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}">
                    <MenuItem Header="Execute Command" 
                              CommandParameter="{Binding DataContext.SelectedItem}"
                              Command="{Binding DataContext.SampleCommand}" />
                </ContextMenu>
            </xcdg:DataGridControl.ContextMenu>
        </xcdg:DataGridControl>

    </Grid>
</Window>

视图模型是:

    namespace MasterDetailSelection
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using Microsoft.Practices.Composite.Presentation.Commands;

    public class ViewModel : INotifyPropertyChanged
    {
        private ObservableCollection<Parent> _parents;
        public event PropertyChangedEventHandler PropertyChanged;
        private DelegateCommand<Object> _sampleCommand;
        private object _selectedItem;

        public ObservableCollection<Parent> Parents
        {
            get { return _parents; }

            set
            {
                _parents = value;
                OnPropertyChanged("Parents");
            }
        }

        public DelegateCommand<Object> SampleCommand
        {
            get
            {
                if (_sampleCommand == null)
                {
                    _sampleCommand = new DelegateCommand<object>(ExecuteSampleCommand, CanExecuteSampleCommand);
                    OnPropertyChanged("SampleCommand");
                }
                return _sampleCommand;
            }
        }

        public bool CanExecuteSampleCommand(Object commandParameter)
        {
            return true;
        }

        public void ExecuteSampleCommand(Object commandParameter)
        {
            Console.WriteLine("ExecuteSampleCommand");
        }

        public object SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                if (_selectedItem != value)
                {
                    _selectedItem = value;
                    OnPropertyChanged("SelectedItem");
                }
            }
        }

        public void LoadParents()
        {
            var parents = new ObservableCollection<Parent>()
                {
                    new Parent()
                        {
                            Id=1, 
                            Description = "Parent1",
                            Children  = new List<Child>(){new Child()    {Id = 1, Description = "Child1"}    }
                        }
                };

            Parents = parents;
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

有两个简单的实体:

public class Parent
{
    public int Id { get; set;}
    public string Description { get; set;}
    public IEnumerable<Child> Children { get; set;}
}
public class Child
{
    public int Id { get; set; }
    public string Description { get; set; }
}

App.xaml.cs中的OnStartup覆盖包含以下内容:

var viewModel = new ViewModel();
var window = new MainWindow();
window.DataContext = viewModel;
viewModel.LoadParents();
window.Show();

每当我选择父行时,将使用填充对象调用SelectedItem setter。当我选择子行时,调用相同的setter,但是使用null值。

当在子行上单击上下文菜单时,有什么方法可以获得对所选项的引用 - 并且在没有代码隐藏的情况下执行此操作。如果没有,是否可以使用代码隐藏?

1 个答案:

答案 0 :(得分:0)

也许您应该直接在单元格或行上设置上下文菜单,如下所示。然后,您可以将适当的模型发送到命令。在下面的示例中,我使用了一个静态定位器类,它具有我的VM来绑定实际命令。

<Style TargetType="{x:Type xcdg:DataCell}">
    <Setter Property="ContextMenu" Value="{StaticResource CellContextMenu}">
        <Setter.Value>
            <ContextMenu>
                <MenuItem Header="Execute Command" 
                          CommandParameter="{Binding}"
                          Command="{Binding Source={StaticResource Locator} Path=ViewModel.SampleCommand}" />
            </ContextMenu>
        </Setter.Value>
    </Setter>
</Style>