WPF datagrid 单击时折叠详细信息行

标签 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;
    }

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

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

最佳答案

要使用“正确”的 MVVM 执行此操作,您应该将 RowDetailsVisibilityMode 绑定(bind)到 View 模型上的属性:

<DataGrid x:Name="dgCompletedJobs" RowDetailsVisibilityMode="{Binding RowDetailsVisible}"/>

您的 View 模型属性将类似于:
private DataGridRowDetailsVisibilityMode _rowDetailsVisible;
public DataGridRowDetailsVisibilityMode RowDetailsVisible
{
    get { return _rowDetailsVisible; }
    set {
        _rowDetailsVisible = value;
        if (PropertyChanged != null) {
             PropertyChanged(this, new PropertyChangedEventArgs("RowDetailsVisible"));
        }
    }
}

要将鼠标单击事件链接到属性的更改,您可以执行一些花哨的附加行为命令,如 here 所示。 ,或者只是使用后面的代码直接调用 View 模型(我经常为简单的任务自己这样做):
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;
    }
}

关于WPF datagrid 单击时折叠详细信息行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13354603/

相关文章:

.net - 在 WPF 绑定(bind)中撤消

c# - MVVM 中的 DataGridComboBoxColumn SelectionChanged 事件

css - CSS边际崩溃?

.net - WPF 中 BitmapFrame 和 BitmapImage 之间的区别

c# - 以编程方式从剪贴板粘贴文件 : Copy or Move?

WPF 按钮样式

java - Vaadin网格与表格

c# - 从枚举自动生成 DataGrid 列

python - 蜘蛛 (Python) : collapse blocks of code

arrays - 将 3d 数组中的工作表行绑定(bind)为 2d 数组