WPF ListView控件的TextBlock TextWrapping [英] WPF ListView TextBlock TextWrapping

查看:254
本文介绍了WPF ListView控件的TextBlock TextWrapping的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立,需要有五列一个ListView - 第一个需要具有文本,可以是任何长度的,需要这样的包装的文本是可见的包裹窗口大小的变化每当(除了改变行的高度)和其他四列是45.静态宽度我一直在寻找这个和每一个解决方案我遇到任何时​​间,需要一个静态的宽度或无法正常工作。

解决方案的尝试:

列汽车,1 *,2 *等的宽度(设置忽略)
DockPanel中(设置忽略)
WrapPanel(忽略)
设置宽于母公司的RelativeSource为ActualWidth的(忽略)

任何想法?这似乎是人的一个显著一些有同样的问题,但我会极力preFER至没有去静态路由宽度此列。特别是因为当我这样做,反正内容只是被切断(即使有高度=自动的行)。整个窗口的宽度可能是1024的小,但也可能是1600+这就是为什么我要动态调整大小。这样,较小的屏幕将内容包装和大屏幕将只显示一条线由于内容相符。

下面是XAML:

 < ListView.ItemTemplate>
    <&DataTemplate的GT;
        <网格和GT;
            < Grid.ColumnDefinitions>
                < ColumnDefinition />
                < ColumnDefinition宽度=45/>
                < ColumnDefinition宽度=45/>
                < ColumnDefinition宽度=45/>
                < ColumnDefinition宽度=45/>
            < /Grid.ColumnDefinitions>            <! - 这是需要来包装其内容的文本块(与
                 改变行的高度(因此全部内容仍
                 可见),以任何可用空间,但不应该
                 统筹的ListView比母体的宽度。 - >
            < TextBlock的文本={结合内容}填充=20,6,6,6/>            <! - 这四个区块会不会有其他的内容,最终,而只需要
                 为45宽 - >
            &所述; TextBlock的文本=XGrid.Column =1的Horizo​​ntalAlignment =中心/>
            &所述; TextBlock的文本=XGrid.Column =2的Horizo​​ntalAlignment =中心/>
            &所述; TextBlock的文本=XGrid.Column =3的Horizo​​ntalAlignment =中心/>
            < TextBlock的文本=XGrid.Column =4的Horizo​​ntalAlignment =中心/>
        < /网格和GT;
     < / DataTemplate中>
< /ListView.ItemTemplate>


解决方案

没那么容易...但它可以做到的。

我为你写的解决方案。总之,使用前pression混合创建的ListView模板的复制和删除的ScrollViewer周边项目presenter。

下面是一个更深入的解释:

<一个href=\"http://www.rhyous.com/2011/07/26/how-to-have-the-textblock-in-a-left-column-of-a-grid-in-a-listview-template-expand-or-shrink-with-text-wrapping/\"相对=nofollow>如何让文本块在一个ListView模板网格的左柱扩大或文字环绕缩水?

I am building a ListView that needs to have five columns - the first one needs to have text that can be any length and needs to wrap whenever the window size changes (in addition to changing the row height so the wrapped text is visible) and the other four columns are a static width of 45. I've been searching for hours on this and every solution I come across either requires a static width or doesn't work.

Solutions tried:

Column widths of auto, 1*, 2*, etc. (settings ignored) DockPanel (settings ignored) WrapPanel (ignored) Setting Width to RelativeSource of parent for ActualWidth (ignored)

Any ideas? It seems like a significant number of people have had this same problem, but I would highly prefer to not have to go the static width route for this column. Especially since the content just gets cut off when I do that anyway (even with height="Auto" for the row). The width of the overall window could be as small as 1024, but could also be 1600+ which is why I want dynamic sizing. That way smaller screens will have the content wrap and larger screens will just show the one line since the content fits.

Here is the XAML:

<ListView.ItemTemplate>
    <DataTemplate>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition Width="45" />
                <ColumnDefinition Width="45" />
                <ColumnDefinition Width="45" />
                <ColumnDefinition Width="45" />
            </Grid.ColumnDefinitions>

            <!-- This is the TextBlock that needs to wrap its content (and
                 change the height of the row (so the full content is still
                 visible) to whatever the available space is, but should not
                 make overall ListView wider than the parent's width. -->
            <TextBlock Text="{Binding Content}" Padding="20,6,6,6" />                            

            <!-- These four blocks will have other content eventually, but only need
                 to be 45 wide -->
            <TextBlock Text="X" Grid.Column="1" HorizontalAlignment="Center" />
            <TextBlock Text="X" Grid.Column="2" HorizontalAlignment="Center" />
            <TextBlock Text="X" Grid.Column="3" HorizontalAlignment="Center" />
            <TextBlock Text="X" Grid.Column="4" HorizontalAlignment="Center" />
        </Grid>
     </DataTemplate>
</ListView.ItemTemplate>

解决方案

Not so easy...but it can be done.

I wrote a solution for you. In short, use Expression Blend to create a copy of the ListView Template and delete the ScrollViewer surrounding the ItemPresenter.

Here is a more indepth explanation:

How to have the TextBlock in a left column of a Grid in a ListView Template expand or shrink with text wrapping?

这篇关于WPF ListView控件的TextBlock TextWrapping的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆