简体   繁体   English

WPF DataGrid列包装

[英]WPF DataGrid column wrapping

I have a datagrid with 4 columns. 我有一个4列的datagrid。 The first three have Width="Auto" , whereas the last has Width="*" to fill the remaining space. 前三个具有Width="Auto" ,最后三个具有Width="*"来填充剩余的空间。 I do not want a horizontal scrollbar so I have disabled. 我不需要水平滚动条,因此已禁用。

例

When the program is resized to a certain amount of width, the above situation happens (only the text of the last column gets wrapped). 当将程序调整为一定的宽度时,会发生上述情况(仅最后一列的文本被换行)。 This doesn't look that bad yet, but when you reduce the window width even more (in which there isn't any space anymore to render the right column), the column with 'wwwwwwwwwwwwwww...' still doesn't wrap it's text and everything becomes white as the right column keeps wrapping to infinity (no space). 看起来还不错,但是当您进一步减小窗口宽度时(其中不再有空间可以显示右侧的列),带有“ wwwwwwwwwwwwwww ...”的列仍不会包裹它文本,并且所有内容都变为白色,因为右列不断换行到无穷大(无空格)。 Now, I expected this problem to go away by introducing a minimum width to the right most column, but instead it results in that column being rendered (partially or fully) outside of the visible area. 现在,我希望通过在最右边的列中引入最小宽度来解决此问题,但是这会导致该列在可见区域之外(部分或全部)呈现。

It works somewhat as expected when I do the third column also as Width="*" but this results in crippling the width of the last column significantly (which I don't want). 当我将第三列也设置为Width="*"时,它的工作效果与预期的一样,但这会导致最后一列的宽度明显下降(我不希望这样做)。 I tried to set it to Width="0.1*" (or anything along these lines) with a minimum width instead, but I get an infinity exception from the datagrid. 我尝试将其设置为Width="0.1*" (或沿这些方向的任何东西),但使用最小宽度,但是我从datagrid中获得了无限异常。

This is driving me nuts and I'm not seeing a way out of here to achieve what I want. 这让我发疯,而且我看不到要实现自己想要的目标的出路。 In short, all I want is the text of the columns to wrap to the available width of the datagrid. 简而言之,我只需要将列的文本包装到datagrid的可用宽度即可。 The last column should have the width preference. 最后一列应具有宽度首选项。

XAML: XAML:

<DataGrid Grid.Row="0" Grid.Column="1" IsEnabled="{Binding HasValidSelectedItem}" ItemsSource="{Binding Items, IsAsync=True}" AutoGenerateColumns="False" 
                                        SnapsToDevicePixels="True" EnableColumnVirtualization="False" HorizontalAlignment="Stretch" CanUserAddRows="False" CanUserDeleteRows="False" 
                                        CanUserResizeRows="False" HorizontalScrollBarVisibility="Disabled" >
    <DataGrid.Resources>
        <Style TargetType="CheckBox" BasedOn="{StaticResource CenteredCheckBoxStyle}" x:Key="CenteredReadOnlyCheckBoxStyle">
            <Setter Property="IsHitTestVisible" Value="False"/>
            <Setter Property="Focusable" Value="False"/>
        </Style>
    </DataGrid.Resources>                
    <DataGrid.Columns>
        <DataGridCheckBoxColumn Header="Test First" 
             Binding="{Binding First, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True" ElementStyle="{StaticResource CenteredReadOnlyCheckBoxStyle}"
             EditingElementStyle="{StaticResource CenteredReadOnlyCheckBoxStyle}" >                        
        </DataGridCheckBoxColumn>
        <DataGridCheckBoxColumn Header="Test Second" 
             Binding="{Binding Second, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True" ElementStyle="{StaticResource CenteredReadOnlyCheckBoxStyle}"
             EditingElementStyle="{StaticResource CenteredReadOnlyCheckBoxStyle}" />
        <DataGridTextColumn Header="Test Third" Binding="{Binding Third}"
             EditingElementStyle="{StaticResource DataGridEditingTextBoxStyle}" CellStyle="{StaticResource DataGridToolTipDataGridCellStyle}">
            <DataGridTextColumn.ElementStyle>
                <Style BasedOn="{StaticResource DataGridTextBlockStyle}" TargetType="{x:Type TextBlock}">
                    <Setter Property="TextWrapping" Value="Wrap"/>
                    <Setter Property="Padding" Value="0,6"/>
                </Style>
            </DataGridTextColumn.ElementStyle>
        </DataGridTextColumn>
        <DataGridTextColumn Header="Test Fourth" Width="*" IsReadOnly="True" Binding="{Binding Path=Fourth}" 
             EditingElementStyle="{StaticResource DataGridEditingTextBoxStyle}" CellStyle="{StaticResource DataGridToolTipDataGridCellStyle}">
            <DataGridTextColumn.ElementStyle>
                <Style BasedOn="{StaticResource DataGridTextBlockStyle}" TargetType="{x:Type TextBlock}">
                    <Setter Property="TextWrapping" Value="Wrap"/>
                    <Setter Property="Padding" Value="0,6"/>
                </Style>
            </DataGridTextColumn.ElementStyle>
        </DataGridTextColumn>

    </DataGrid.Columns>
</DataGrid> 

If you don't want a horizontal scrollbar, and it sounds like you want the third and fourth columns to autosize (and wrap if necessary), then don't use Width="Auto" . 如果您不希望水平滚动条,并且听起来像您希望第三和第四列自动调整大小(并在必要时进行换行),则不要使用Width="Auto" It seems to me that you would want a fixed column width for the first two (something like Width="100" , then for the third and fourth columns use something like Width="40*" and Width="60*" respectively. (Or set them to whatever ratio you want) In addition, you could set a MinWidth and/or a MaxWidth to control the expansion of the columns. 在我看来,您希望前两列的列宽固定(例如Width="100" ,然后第三列和第四列分别使用Width="40*"Width="60*" 。 (或将它们设置为所需的任何比率)此外,您可以设置MinWidth和/或MaxWidth来控制列的扩展。

To enable the wrapping, you'll just need to add an ElementStyle to the DataGridTextColumn . 要启用包装,您只需要向DataGridTextColumn添加ElementStyle即可。

<DataGrid>
    <DataGrid.Columns>
        <DataGridCheckBoxColumn Header="Check1" Binding="{Binding Check1}" Width="100" />
        <DataGridCheckBoxColumn Header="Check2" Binding="{Binding Check2}" Width="100" />
        <DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="40*" MaxWidth="200">
            <DataGridTextColumn.ElementStyle>
                <Style>
                    <Setter Property="TextBlock.TextWrapping" Value="Wrap"></Setter>
                </Style>
            </DataGridTextColumn.ElementStyle>
        </DataGridTextColumn>
        <DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="60*" MinWidth="200">
            <DataGridTextColumn.ElementStyle>
                <Style>
                    <Setter Property="TextBlock.TextWrapping" Value="Wrap"></Setter>                     
                </Style>
            </DataGridTextColumn.ElementStyle>
        </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM