【问题标题】:How to stop DataGrid collapsing RowDetails on property change?如何停止 DataGrid 在属性更改时折叠行详细信息?
【发布时间】:2016-03-18 13:12:02
【问题描述】:

给定一个带有切换行详细信息按钮的 DataGrid。为什么每次更改属性时都会折叠详细信息?

每当列使用时,它都会折叠详细信息:

UpdateSourceTrigger=LostFocus

当属性在详细信息视图中更新时,它也会折叠它。

有没有办法让它保持打开状态?该行仍处于选中状态。

XAML:

RowDetailsVisibilityMode="Collapsed"

                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button x:Name="buttonDetails" Content="Hello" ButtonBase.Click="Details_Click"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

C#:

    // Load stuff from db
    _context.JobCollection.Load();

    // Set source with db stuff
    jobViewSource.Source = _context.JobCollection.Local;



    private void Details_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            // the original source is what was clicked.  For example 
            // a button.
            DependencyObject dep = (DependencyObject)e.OriginalSource;

            // iteratively traverse the visual tree upwards looking for
            // the clicked row.
            while ((dep != null) && !(dep is DataGridRow))
            {
                dep = VisualTreeHelper.GetParent(dep);
            }

            // if we found the clicked row
            if (dep != null && dep is DataGridRow)
            {
                // get the row
                DataGridRow row = (DataGridRow)dep;

                // change the details visibility
                if (row.DetailsVisibility == Visibility.Collapsed)
                {
                    row.DetailsVisibility = Visibility.Visible;
                }
                else
                {
                    row.DetailsVisibility = Visibility.Collapsed;
                }
            }
        }
        catch (System.Exception)
        {
        }
    }

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

    一些建议的更改:

    1. RowDetailsVisibilityMode="Collapsed" 应用到您的DataGrid。

    2. 你的按钮点击应该是这样的:

      private void Details_Click(object sender, RoutedEventArgs e)
      {
         DataGridRow row = (DataGridRow)Dgrd1.ItemContainerGenerator.ContainerFromItem(Dgrd1.SelectedItem);
      
         if (row.DetailsVisibility == System.Windows.Visibility.Visible)
             row.DetailsVisibility = System.Windows.Visibility.Collapsed;
         else
             row.DetailsVisibility = System.Windows.Visibility.Visible;
      }
      

    【讨论】:

    • 已经有了这个。不回答问题。还是谢谢。
    • 发布您的 XAML 代码。或者在 Dropbox 中发布您的代码并在评论中发布链接。
    • @flux 我已经按照您的描述制作了一个示例应用程序。
    • 你看到问题了吗?
    猜你喜欢
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    • 2012-09-27
    • 2011-08-01
    • 1970-01-01
    相关资源
    最近更新 更多