WPF Data grid Text Ellipsis Not working

馋奶兔 提交于 2019-12-10 22:46:35

问题


I have a column with long user comments. I load it using following code...

<my:DataGridTextColumn Header="Message"
                       Binding="{Binding UserMessage, Mode=OneWay}" 
                       CanUserSort="True">
    <my:DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}"
               BasedOn="{StaticResource {x:Type TextBlock}}">
               <Setter Property="TextWrapping"
                       Value="NoWrap" />
               <Setter Property="TextTrimming"
                       Value="CharacterEllipsis"/>                                    
               <Setter Property="ToolTip"
                       Value="{Binding Path=UserMessage, Mode=OneWay}"/>
        </Style>
    </my:DataGridTextColumn.ElementStyle>
</my:DataGridTextColumn>

But the ellipsis wont work. The column continues to display long text of data. Also when I set the width of the textblock explicitly to some value then the ellipsis work fine but when I resize my column it wont show any more text in it.

Isnt there a starighforward way to do this?

Thx Vinit Sankhe.


回答1:


Try setting widths on your columns that only need static widths. On this column you set the width to "*"

<my:DataGridTextColumn Header="Message"
                       Binding="{Binding UserMessage, Mode=OneWay}" 
                       CanUserSort="True"
                       Width="*">
    <my:DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}"
               BasedOn="{StaticResource {x:Type TextBlock}}">
               <Setter Property="TextWrapping"
                       Value="NoWrap" />
               <Setter Property="TextTrimming"
                       Value="CharacterEllipsis"/>                                    
               <Setter Property="ToolTip"
                       Value="{Binding Path=UserMessage, Mode=OneWay}"/>
        </Style>
    </my:DataGridTextColumn.ElementStyle>
</my:DataGridTextColumn>

I found your question by Googling your question. I took your code and just added widths to my columns (except for a "Title" column) and was able to have it place the ellipsis correctly. I also added a MinWidth just to make sure that when the window is resized the column isn't squished to nothing.




回答2:


Have a look at this article. It think it's the solution you're looking for:



来源:https://stackoverflow.com/questions/3510029/wpf-data-grid-text-ellipsis-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!