如何让 ListView(或 DataGrid)与 TextWrapping (WPF) 一起使用

发布于 2024-07-13 14:49:33 字数 615 浏览 3 评论 0原文

我的意思是,我在其中一列中有一个控件和文本。 通常,如果用户更改列大小,则只是剪切文本。 如果我尝试将模板与文本块或文本框一起使用并在模板内部设置 TextWrapping="Wrap" - 它实际上不会改变任何内容。 出路何在?

*是的,我在提问之前进行了搜索,并在该网站上发现了类似的问题。 但没有答案 ((( *


安迪,谢谢你的回答,尽管我自己也认为它应该这样做。 我检查了你的简单变体并且它有效。 虽然我有一个类似的例子,但没有成功。 唯一的区别是,在模板中,我使用了 UserControl,它的 Content 属性内有 TextBlock 和 StackPanel 内的其他控件。 经过一个小时在代码的不同部分尝试 Width、Horizo​​ntalAlignment 等之后,我明白了,我无能为力。 然后 - 我刚刚用 Grid 更改了 StackPanel。 还有——中提琴! - 有用。 我不知道这是一个错误还是只是 StackPanel 的一些特定反应(实际上我在运行时检查了它的参数 - 这很奇怪:ActualWidth 大于 DesiredSize 和面板的实际宽度)。 所以如果有人能告诉我 - 为什么会这样? - 这会很有趣。 如果没有 - 我将只使用带有网格的版本。

I mean, i have one of this controls and text inside one of it's columns.
Usually if user changes column size, it's just cutting text. If I'm trying to use template with textblock or textbox and set TextWrapping="Wrap" inside of the template - it's really doesn't change anything.
What's the way out?

*And yes, I searched before asking, and found similar questions on this site. But without answer ((( *


Andy, thx for answer, though I'm myself was thinking that it should act that way.
I ckecked your simple variant and it works. Though I have a similar example and it didn't worked. The only difference was that in template I used my UserControl that have TextBlock and other controls inside the StackPanel inside it's Content Property. And after hour of experimenting with Width, HorizontalAlignment and so on in different part of the code I understand, that I can do nothing.
And then - i just changed StackPanel with Grid. And - viola! - it works. I don't know if it's a bug or just some specific reaction of StackPanel (actually I checked it parameters in run-time - and it was very strange: ActualWidth was more then the DesiredSize and real width of the Panel). So if someone could tell me - why is this so? - it would be interesting. If no - i'll just use version with Grid.

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

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

发布评论

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

评论(2

猫七 2024-07-20 14:49:33

以下内容对我有用:

<ListView ItemsSource="{Binding Source={StaticResource MyItems}}">
    <ListView.View>
        <GridView>
            <GridViewColumn Width="50" Header="Column 1">
                <GridViewColumn.CellTemplate>
                    <DataTemplate x:Name="col1Template">
                        <TextBlock TextWrapping="WrapWithOverflow" Text="{Binding Path=Column1Text}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            ...
        </GridView>
    </ListView.View>
</ListView>

如果列的宽度不足以显示所有文本,这会导致第一列中的文本换行为多行。

The following worked for me:

<ListView ItemsSource="{Binding Source={StaticResource MyItems}}">
    <ListView.View>
        <GridView>
            <GridViewColumn Width="50" Header="Column 1">
                <GridViewColumn.CellTemplate>
                    <DataTemplate x:Name="col1Template">
                        <TextBlock TextWrapping="WrapWithOverflow" Text="{Binding Path=Column1Text}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            ...
        </GridView>
    </ListView.View>
</ListView>

This caused the text in the first column to wrap onto multiple lines if the width of the column wasn't wide enough to show all of the text.

又爬满兰若 2024-07-20 14:49:33

确保您没有定义 datagrid RowHeight,否则它将在文本换行后覆盖文本块的“真实”高度。

Make sure you don't have datagrid RowHeight defined or it is going to override the "true" height of the textblock after it has been textwrapped.

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