13

假设我有一个这样定义的 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 的情况下看起来像这样

替代文字

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

我尝试过的东西可以实现包装但不是令人满意的方式

  • 在 Border 或 TextBlock 上设置 Width 或 MaxWidth。不是很动态。
  • 在 DataGrid 上设置 ScrollViewer.Horizo​​ntalScrollBarVisibility="Disabled"。当列不适合时不是很好。
4

6 回答 6

15

这里的答案感觉像是一种解决方法,所以我做了一些研究并在 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>

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

于 2011-10-21T09:42:54.663 回答
14

这就是我最终做的。我宁愿为此使用 DataGrid 上的属性,但由于不存在这样的属性,我需要一种解决方法。

替代文字

首先,我只使用了父 DataGrid 中的 ActualWidth 并删除了一个常量 9。这最初是有效的,但是当垂直滚动条变得可见时失败了,所以我不得不使用 MultiBinding。

<DataGrid.RowDetailsTemplate>
    <DataTemplate>
        <Border HorizontalAlignment="Left" CornerRadius="5"
                BorderBrush="Red" BorderThickness="2" Background="Black">
            <Border.Width>
                <MultiBinding Converter="{StaticResource RowDetailsWidthMultiConverter}"
                              ConverterParameter="9">
                    <Binding RelativeSource="{RelativeSource AncestorType={x:Type DataGrid}}"
                             Path="ActualWidth"/>
                    <Binding RelativeSource="{RelativeSource AncestorType={x:Type ScrollViewer}}"
                             Path="ComputedVerticalScrollBarVisibility"/>
                </MultiBinding>
            </Border.Width>
            <TextBlock Foreground="White" Text="{Binding RowDetails}" TextWrapping="Wrap"/>
        </Border>
    </DataTemplate>
</DataGrid.RowDetailsTemplate>

在转换器中,我使用了另一个常量 (16) 来补偿可见的垂直滚动条(如果它是可见的)。

public class RowDetailsWidthMultiConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double originalWidth = (double)values[0];
        Visibility verticalScrollbarVisibility = (Visibility)values[1];
        double subtractWidth = System.Convert.ToDouble(parameter);
        double returnWidth = originalWidth - subtractWidth;
        if (verticalScrollbarVisibility == Visibility.Visible)
        {
            return returnWidth - 16;
        }
        return returnWidth;
    }
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
}

更新

我对解决方案进行了一些改进,对 ItemsPresenter 使用 ActualWidth 而不是 DataGrid(其中 ActualWidth 不会根据可见的 ScrollBar 而改变),因此不再需要 MultiConverter 和两个常量。

<DataGrid.Resources>
    <local:SubtractConstantConverter x:Key="SubtractConstantConverter"/>
</DataGrid.Resources>
<DataGrid.RowDetailsTemplate>
    <DataTemplate>
        <Border HorizontalAlignment="Left" CornerRadius="5"
                BorderBrush="Red" BorderThickness="2" Background="Black"
                Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type ItemsPresenter}},
                                Path=ActualWidth,
                                Converter={StaticResource SubtractConstantConverter},
                                ConverterParameter=6}">
            <TextBlock Foreground="White" Text="{Binding RowDetails}" TextWrapping="Wrap"/>
        </Border>
    </DataTemplate>
</DataGrid.RowDetailsTemplate>

减法常数转换器

public class SubtractConstantConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double originalValue = (double)value;
        double subtractValue = System.Convert.ToDouble(parameter);
        return originalValue - subtractValue;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
}
于 2010-11-18T15:08:13.490 回答
4

这就是我最终要做的:将行详细信息宽度与其演示者的实际宽度绑定,然后添加一个具有不同厚度的边框以补偿演示者中垂直滚动条的存在/不存在。这种方法对我来说非常有效。示例 xml:

<DataGrid.RowDetailsTemplate>
     <DataTemplate>
        <Border BorderThickness="2,2,8,2"
                Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type ItemsPresenter}}, Path=ActualWidth}"
                HorizontalAlignment="Left" >
           <!-- add the row details view contents here -->
         </Border>
     </DataTemplate>
</DataGrid.RowDetailsTemplate>
于 2015-05-18T09:19:58.657 回答
2

谢谢Meleak,您的解决方案对我来说效果很好。我们 WPF 新手的一个小补充。请务必将您的 Converter 类声明为资源,以便可以在 Binding 表达式中引用它。

我把我的放在 App.Xaml 中,如下所示:

<Application x:Class="ISCBilling.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:conv="clr-namespace:my.application.namespace"
             StartupUri="IscBillingWindow.xaml">
    <Application.Resources>

        <conv:RowDetailsWidthMultiConverter x:Key="RowDetailsWidthMultiConverter" />

    </Application.Resources>
</Application>
于 2010-12-29T19:00:48.693 回答
2

您也许可以将 MaxWidth 绑定到ElementName=PART_ColumnHeadersPresenter, Path=ActualWidth或 RenderSize.Width。我相信这是显示列的 DataGrid 模板的一部分,所以理论上它应该可以工作

于 2010-11-18T14:07:34.187 回答
1

为了节省其他人一些头疼和试错时间:

在对 Fredrik Hedblad 的最新(1/1/11)解决方案大惊小怪一段时间后,我发现ConverterParameter值应该是6 + [left margin} + [right margin](即模板中最外层容器的边距。)在检查了屏幕截图的放大图后,我预计6是每行左侧垂直条的宽度。

于 2011-06-15T23:46:58.633 回答