在RowDetailsTemplate中使用DataGrid的WPF DataGrid

时间:2013-09-24 16:16:00

标签: wpf mvvm datagrid

我之前关于检测VM中的属性更改的帖子不够深入,所以我发布了这个

我有一个乔布斯网格。每份工作都可以有一名或多名员工。

DataGrid的RowDetailsTemplate包含另一个显示员工的网格。因此,父网格绑定到作业列表。内部网格绑定到作业模型上的Employees列表。

工作模式:

public class Job : _Base
{
    private string _JobName = string.Empty;
    public string JobName
    {
        get { return _JobName; }
        set 
        {
            if (_JobName != value)
            {
                _JobName = value;
                RaisePropertyChanged("JobName");
            }
        }
    }

    private string _JobNumber = string.Empty;
    public string JobNumber
    {
        get { return _JobNumber; }
        set
        {
            if (_JobNumber != value)
            {
                _JobNumber = value;
                RaisePropertyChanged("JobNumber");
            }
        }
    }

    private ObservableCollection<Employee> _Employees;
    public ObservableCollection<Employee> Employees
    {
        get { return _Employees; }
        set
        {
            if (_Employees != value)
            {
                if (_Employees != value)
                {
                    _Employees = value;
                    RaisePropertyChanged("Employees");
                }
            }
        }
    }

    private Employee _SelectedEmployee;
    public Employee SelectedEmployee
    {
        get { return _SelectedEmployee; }
        set
        {
            if (_SelectedEmployee != value)
            {
                if (_SelectedEmployee != value)
                {
                    _SelectedEmployee = value;
                    RaisePropertyChanged("SelectedEmployee");
                }
            }
        }
    }

    public Job()
    {
        Employees = new ObservableCollection<Employee>();
    }
}

员工模式

public class Employee : _Base
{
    private string _EmployeeName = string.Empty;
    public string EmployeeName
    {
        get { return _EmployeeName; }
        set
        {
            if (_EmployeeName != value)
            {
                _EmployeeName = value;
                RaisePropertyChanged("EmployeeName");
            }
        }
    }

    private bool _IsChecked = false;
    public bool IsChecked
    {
        get { return _IsChecked; }
        set
        {
            if (_IsChecked != value)
            {
                _IsChecked = value;
                RaisePropertyChanged("IsChecked");
            }
        }
    }
}        

XAML

<DataGrid ItemsSource="{Binding Jobs}"
            SelectedItem="{Binding SelectedJob}"
            AutoGenerateColumns="False">

    <DataGrid.Columns>
        <DataGridTextColumn Header="Job Name" Binding="{Binding JobName}" />
        <DataGridTextColumn Header="Job Number" Binding="{Binding JobNumber}" />
    </DataGrid.Columns>

    <DataGrid.RowDetailsTemplate>
        <DataTemplate>

            <StackPanel Orientation="Vertical">

                <DataGrid ItemsSource="{Binding Employees}"
                            SelectedItem="{Binding SelectedEmployee}"
                            AutoGenerateColumns="False">

                    <DataGrid.Columns>
                        <DataGridCheckBoxColumn Binding="{Binding IsChecked}"/>
                        <DataGridTextColumn Binding="{Binding EmployeeName}"/>
                    </DataGrid.Columns>

                </DataGrid>

                <Button Margin="5"
                        Height="23"
                        Width="75"
                        HorizontalAlignment="Left"
                        Content="Remove"/>

            </StackPanel>


        </DataTemplate>
    </DataGrid.RowDetailsTemplate>

</DataGrid>

MainWindowViewModel

public class MainWindowViewModel : _Base
{
    private ObservableCollection<Job> _Jobs;
    public ObservableCollection<Job> Jobs
    {
        get { return _Jobs; }
        set 
        {
            if (_Jobs != value)
            {
                if (_Jobs != value)
                {
                    _Jobs = value;
                    RaisePropertyChanged("Jobs");
                }
            }
        }
    }

    private Job _SelectedJob;
    public Job SelectedJob
    {
        get { return _SelectedJob; }
        set
        {
            if (_SelectedJob != value)
            {
                if (_SelectedJob != value)
                {
                    _SelectedJob = value;
                    RaisePropertyChanged("SelectedJob");
                }
            }
        }
    }

    public MainWindowViewModel()
    {
        this.PropertyChanged += new PropertyChangedEventHandler(MainWindowViewModel_PropertyChanged);
    }

    void MainWindowViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        if (e.PropertyName.Trim().ToLower() == "ischecked")
        {
            int x = 1;
        }
    }
}

我有几个问题:

1)单击内部网格中的员工时,作业模型上的SelectedEmployee属性不会触发。

2)选择员工时,不会触发MainWindowViewModel_PropertyChanged。

3)注意内部网格下面的按钮。如何将其命令绑定到MainWindowVM?

1 个答案:

答案 0 :(得分:1)

  1. 由于你有DataGrid inside DataGrid's row,所以上面的DataGrid不知何故eating up the selectionchange for the inner DataGrid。要解决此问题,您需要强制更新子DataGrid的绑定源。为此,捕获内部DataGrid的SelectionChanged事件,并在处理程序中执行以下操作。

     private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
     {
          DataGrid grid = e.OriginalSource as DataGrid;
          var expression = grid.GetBindingExpression(DataGrid.SelectedItemProperty);
          expression.UpdateSource();
    
      }
    
  2. MainwindowVM没有任何缺陷属性,这就是为什么它的属性没有被触发的原因。

  3. 要解决这个问题,您需要做几件事。问题是DataGrid单元格和行不继承DataContext,因为它们不在其可视树下。因此,要解决此问题,您必须使用BindingProxy将Windows DataContext带到DataGrid的rowdetails中的按钮

  4. 定义Binding Proxy类,如下所示:

        public class MyBindingProxy : Freezable
        {
            public static readonly DependencyProperty BindingDataProperty =
                DependencyProperty.Register("BindingData", typeof(object),
                typeof(MyBindingProxy), new UIPropertyMetadata(null));
    
            protected override Freezable CreateInstanceCore()
            {
                return new MyBindingProxy();
            }
    
            public object BindingData
            {
                get { return (object)GetValue(BindingDataProperty); }
                set { SetValue(BindingDataProperty, value); }
            }
    
        }
    

    现在在窗口的资源中(DataGrid所在的位置)创建代理实例并将BindingData设置为Window的DataContext,即MainWindowViewModel:

    <Window.Resources>
        <local:MyBindingProxy x:Key="myproxy" BindingData="{Binding}" />
    </Window.Resources>
    

    现在只需在按钮上设置如下命令:

    <Button Margin="5"
       Height="23"
       Width="75"
       HorizontalAlignment="Left"
       Content="Remove"
       Command="{Binding BindingData.MyCommand, Source={StaticResource myproxy}}"/>