单击时,WPF数据网格折叠详细信息行

Yel*_*nic 3 wpf datagrid collapse rowdetails

我需要在用户单击它时折叠WPF DataGrid的详细信息行,并在再次单击时重新显示它.我还想使用单选来保留VisibleWhenSelected的DataGridRoDetailsVisibilityMode.

我想出了这个解决方案,基于其他地方的帖子:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0a45b3a7-46d0-45a9-84b2-0062f07f6fec#eadc8f65-fcc6- 41df-9ab9-8d93993e114c

    private bool _rowSelectionChanged;


    private void dgCompletedJobs_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        _rowSelectionChanged = true;
    }

    private void dgCompletedJobsMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        DependencyObject dep = (DependencyObject)e.OriginalSource;

        //navigate up the tree
        while (dep != null &&
            !(dep is DataGridCell) &&
            !(dep is DataGridColumnHeader))
        {
            dep = VisualTreeHelper.GetParent(dep);
        }

        if (dep == null)
        {
            return;
        }

        DataGridCell dgc = dep as DataGridCell;
        if (dgc != null)
        {
            //navigate further up the tree
            while (dep != null && !(dep is DataGridRow))
            {
                dep = VisualTreeHelper.GetParent(dep);
            }

            DataGridRow dgr = dep as DataGridRow;
            DataGrid dg = sender as DataGrid;
            if (dg != null && dgr != null)
            {
                if (dgr.IsSelected && !_rowSelectionChanged)
                {
                    dg.RowDetailsVisibilityMode =
                        (dg.RowDetailsVisibilityMode == DataGridRowDetailsVisibilityMode.VisibleWhenSelected)
                            ? DataGridRowDetailsVisibilityMode.Collapsed
                            : DataGridRowDetailsVisibilityMode.VisibleWhenSelected;
                }
                else
                {
                    dg.RowDetailsVisibilityMode = DataGridRowDetailsVisibilityMode.VisibleWhenSelected;
                }
            }
        }
        _rowSelectionChanged = false;
    }
Run Code Online (Sandbox Code Playgroud)

这似乎很好地解决了我的问题,但我怀疑这可以更简单和优雅地完成,特别是因为我在这个项目上使用MVVM.但是,我认为这是事件驱动的代码隐藏的可接受用法,因为它纯粹是表示逻辑.

有没有人有更清洁的解决方案?

Ste*_*olt 5

要使用"正确的"MVVM执行此操作,您应该将RowDetailsVisibilityMode绑定到视图模型上的属性:

<DataGrid x:Name="dgCompletedJobs" RowDetailsVisibilityMode="{Binding RowDetailsVisible}"/>
Run Code Online (Sandbox Code Playgroud)

您的视图模型属性将类似于:

private DataGridRowDetailsVisibilityMode _rowDetailsVisible;
public DataGridRowDetailsVisibilityMode RowDetailsVisible
{
    get { return _rowDetailsVisible; }
    set {
        _rowDetailsVisible = value;
        if (PropertyChanged != null) {
             PropertyChanged(this, new PropertyChangedEventArgs("RowDetailsVisible"));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

要将鼠标单击事件链接到更改属性,您可以按照此处的说明执行某些花哨的附加行为,或者只使用后面的代码直接调用视图模型(我经常为简单任务执行此操作):

private void dgCompletedJobsMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    Window1ViewModel viewModel = (Window1ViewModel)DataContext;
    if (viewModel.RowDetailsVisible == DataGridRowDetailsVisibilityMode.Collapsed) {
        viewModel.RowDetailsVisible = DataGridRowDetailsVisibilityMode.VisibleWhenSelected;
    } else {
        viewModel.RowDetailsVisible = DataGridRowDetailsVisibilityMode.Collapsed;
    }
}
Run Code Online (Sandbox Code Playgroud)


Gra*_*fix 1

为什么不使用发送者参数?如果事件是在 DataGrid 上定义的,则发送者始终是 DataGrid!使用安全强制转换检查 null 是安全的,但这应该可以解决问题。

当您通过可视化树从原始源返回到 DataGrid 时,代码似乎不必要地复杂。

        private void dataGridMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        DataGrid dg = sender as DataGrid;
        if (dg == null)
            return;
        if (dg.RowDetailsVisibilityMode == DataGridRowDetailsVisibilityMode.VisibleWhenSelected)
            dg.RowDetailsVisibilityMode = DataGridRowDetailsVisibilityMode.Collapsed;
        else
            dg.RowDetailsVisibilityMode = DataGridRowDetailsVisibilityMode.VisibleWhenSelected;
    }
Run Code Online (Sandbox Code Playgroud)