wpf - DataGrid RowDetails 宽度问题

标签 wpf datagrid wpfdatagrid rowdetails

假设我有一个像这样定义的 DataGrid

<DataGrid AreRowDetailsFrozen="True"
          ItemsSource="{Binding MyCollection}"
          AutoGenerateColumns="False">
    <DataGrid.RowDetailsTemplate>
        <DataTemplate>
            <Border CornerRadius="5" BorderBrush="Red"
                    BorderThickness="2" Background="Black">
                <TextBlock Foreground="White" Text="{Binding RowDetails}"
                           TextWrapping="Wrap"/>
            </Border>
        </DataTemplate>
    </DataGrid.RowDetailsTemplate>
    <DataGrid.Columns>
        <DataGridTextColumn Header="0" Binding="{Binding Value1}"/>
        <DataGridTextColumn Header="1" Binding="{Binding Value2}"/>
        <DataGridTextColumn Header="2" Binding="{Binding Value3}"/>
        <DataGridTextColumn Header="3" Binding="{Binding Value4}"/>
    </DataGrid.Columns>
</DataGrid>

看起来像这样有和没有 RowDetails

alt text

在右边的图片中,我得到了一个非常长的 DataGridRow,它从不换行。
是否可以让 RowDetails 使用与 DataGrid 相同的宽度而不影响宽度本身?

我尝试过的东西可以实现包装但不是令人满意的方式
  • 在边框或 TextBlock 上设置 Width 或 MaxWidth。不是很动态。
  • 在 DataGrid 上设置 ScrollViewer.Horizo​​ntalScrollBarVisibility="Disabled"。当列不适合时不太好。
  • 最佳答案

    这里的答案感觉像是一种解决方法,所以我做了一些研究,并在 Telerik 论坛上找到了解决方案,因为我们使用了他们的 RadGridView。结果证明该解决方案也适用于 DataGrid。

    关键是将 ScrollViewer.Horizo​​ntalScrollBarVisibility 属性设置为 Disabled,请参见下面的示例。

    <DataGrid ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <DataGrid.RowDetailsTemplate>
        <DataTemplate>
            <Border>
                <TextBlock Foreground="White" Text="{Binding RowDetails}"
                           TextWrapping="Wrap"/>
            </Border>
        </DataTemplate>
    </DataGrid.RowDetailsTemplate>
    

    编辑:
    一个副作用是,如果列需要更多的水平空间而不是它们将被剪裁的空间。因此,如果这是一个问题,那么此解决方案不是最佳的。

    关于wpf - DataGrid RowDetails 宽度问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4210480/

    相关文章:

    c# - 如何从我自己的 .cs 文件访问 WPF MainWindow 控件

    javascript - QueryReadStore 将 JSON 加载到 DataGrid 中,但 JsonRestStore 不会(来自同一来源)

    c# - CommandParameter 多重绑定(bind)返回 DataGrid 控件和所选行中的自定义对象

    项目编辑/更新后的 WPF Datagrid 更新分组

    WPF TreeView 和复选框

    WPF:自定义窗口

    WPF DataGrid - 为什么额外的列

    c# - 如何获取 WPF DataGrid 选定的单元格值?

    c# - 在 MVVM 中对绑定(bind)到 DataGrid 的 ObservableCollection 进行排序

    带有 DataGrid 的 WPF 应用程序中的 C# 搜索框