WPF 数据网格文本省略号不起作用

发布于 2024-09-15 00:26:48 字数 976 浏览 1 评论 0原文

我有一个专栏,里面有很长的用户评论。我使用以下代码加载它...

<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>

但是省略号不起作用。该列继续显示长文本数据。此外,当我将文本块的宽度显式设置为某个值时,省略号可以正常工作,但是当我调整列大小时,它不会在其中显示更多文本。

难道没有一种直观的方法可以做到这一点吗?

谢谢 维尼特·桑赫。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戈亓 2024-09-22 00:26:48

尝试在只需要静态宽度的列上设置宽度。在此列中,您将宽度设置为“*”,

<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>

我通过谷歌搜索您的问题找到了您的问题。我获取了您的代码,只是向我的列添加了宽度(“标题”列除外),并且能够正确放置省略号。我还添加了一个 MinWidth 只是为了确保当调整窗口大小时,该列不会被压扁。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文