如何获得与明星定义为剪辑内容WPF网格列? [英] How do I get WPF Grid columns defined with star to clip content?

查看:137
本文介绍了如何获得与明星定义为剪辑内容WPF网格列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用明星例如比例Grid控件

I have a Grid control that is proportioned using star e.g.

<Grid.ColumnDefinitions>
    <ColumnDefinition Width="50*" />
    <ColumnDefinition Width="100*" />
    <ColumnDefinition Width="50*" />
</Grid.ColumnDefinitions>



不过把一个长的TextBlock 在网格该溢出会导致心烦的比例。 。例如

However putting a long TextBlock in the grid that overflows causes the proportions to be upset. e.g.

<TextBlock Text="Foo" Grid.Column="0" />
<TextBlock Text="Some long text here which overflows" Grid.Column="1" />
<TextBlock Text="Foo" Grid.Column="2" />

这导致中央柱是多于其他两个一倍。我如何保持规定的比例?是否有可能夹的内容?

This causes the central column to be more than double the other two. How do I maintain the specified proportions? Is it possible to clip the content?

我已经设置 TextTrimming =CharacterEllipsis的TextBlocks ,但没有运气。

I have set TextTrimming="CharacterEllipsis" on the TextBlocks but no luck.

修改

重要的是现在看来,网格是在的DataTemplate 粘贴下面来观察行为,

Crucially it seems, the Grid is inside a DataTemplate, paste the following to observe the behaviour,

<!-- FallbackValue is just a quick hack to get some rows to show at design-time -->
<ListBox ItemsSource="{Binding Foo, FallbackValue=1234}"
             HorizontalContentAlignment="Stretch">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="50*" />
                        <ColumnDefinition Width="100*" />
                        <ColumnDefinition Width="50*" />
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="Foo" Grid.Column="0" />
                    <TextBlock Text="Some long text here which overflows" TextTrimming="CharacterEllipsis" Grid.Column="1" />
                    <TextBlock Text="Foo"  Grid.Column="2" />
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

之所以说这是很重要的是,我有另一个电网作为同级的的ListBox 这显示在的ListBox 如图所示,列标题如下,

The reason why this is important is that I have another Grid as a sibling of the ListBox which displays the 'headers' for the columns shown in the ListBox as follows,

<Grid>
    ... Headers and column definitions here
</Grid>

<ListBox ...>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                ... Matching column definitions here
            </Grid>
        </DateTemplate>
    </ListBox.ItemTemplate>
</ListBox>

和,所以很重要的列匹配。

and so it is important that the columns match up.

我试图绑定 ColumnDefinitions 中的的DataTemplate 外部电网 ColumnDefinitions ,但我不能轻易的绑定引用。

I have tried to bind the ColumnDefinitions inside the DataTemplate to the external Grid ColumnDefinitions but I cannot get easily a binding reference to it.

推荐答案

这是与WPF最恼人的问题之一。因为可用的空间产生的模板网格是无限的,因为它需要的实际内容将尽可能多的空间。

This is one of the most annoying problems with WPF. Since the available space yielded to the templated grid is infinite, the actual content will take as much space as it wants.

的最简单的方法是解决一定的宽度向格,但只能解决那里没有调整大小的情况。

The simplest way is to fix a certain width to the Grid, but that solves only the situations where there's no resizing.

而要伸展列表框尺寸(宽,在具体的),可惜我想,有没有比一个自定义转换器没有更好的其他解决方案。

Whereas you want to stretch the ListBox size (width, in the specific), unfortunately I guess that there's no any better solution other than a custom converter.

下面是我的解决方案:

<Window.Resources>
    <local:MyConv x:Key="cv1" />
</Window.Resources>
<Grid>
    <ListBox 
        ItemsSource="{Binding Foo, FallbackValue=1234}"
        HorizontalContentAlignment="Stretch"
        >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox}, Converter={StaticResource cv1}}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="50*" />
                        <ColumnDefinition Width="100*" />
                        <ColumnDefinition Width="50*" />
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="Foo" Grid.Column="0" />
                    <TextBlock Text="Some long text here which overflows" TextTrimming="CharacterEllipsis" Grid.Column="1" />
                    <TextBlock Text="Foo"  Grid.Column="2" />
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>



和转换器:

class MyConv : IValueConverter
{
    public object Convert(
        object value, 
        Type targetType, 
        object parameter, 
        System.Globalization.CultureInfo culture
        )
    {
        return (double)value - 30.0;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

这篇关于如何获得与明星定义为剪辑内容WPF网格列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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