c# - 如何获取用星号定义的 WPF 网格列来剪辑内容?

标签 c# wpf xaml

我有一个 Grid 控件,它使用星号来成比例,例如

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

然而,在溢出的网格中放置一个长的 TextBlock 会导致比例被打乱。例如

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

这导致中心列比其他两个多一倍以上。如何保持指定的比例?是否可以剪辑内容?

我已经在 TextBlocks 上设置了 TextTrimming="CharacterEllipsis" 但没有成功。

编辑

看起来很重要,网格在 DataTemplate 中,粘贴以下内容以观察行为,

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

这很重要的原因是我有另一个 Grid 作为 ListBox 的兄弟,它显示 中显示的列的“标题” ListBox如下,

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

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

所以列匹配很重要。

我试图将 DataTemplate 中的 ColumnDefinitions 绑定(bind)到外部 Grid ColumnDefinitions 但我无法获取很容易成为对它的绑定(bind)引用。

最佳答案

这是 WPF 最烦人的问题之一。由于提供给模板化网格的可用空间是无限的,因此实际内容将占用任意多的空间。

最简单的方法是为 Grid 固定一定的宽度,但这只能解决没有调整大小的情况。

虽然您想拉伸(stretch) ListBox 的大小(具体来说是宽度),但不幸的是,我猜想除了自定义转换器之外没有更好的解决方案。

这是我的解决方案:

<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();
    }
}

关于c# - 如何获取用星号定义的 WPF 网格列来剪辑内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25567474/

相关文章:

xaml - 如何在 XAML Winrt 中使用没有空格的 TextBlock 中的运行?

c# - Web API 路由被 MVC 忽略和处理

c# WPF App.Current 在自定义对话框窗口关闭后变为空

c# - 将字符串绑定(bind)到 TextBlock WPF - 我做错了什么?

WPF 列表框错误模板

c# - Wpf Datagrid 在绑定(bind)时不显示一列

C# 脚本与 C# 编程

c# - 将 System.Web.UI.WebControls.Image 转换为 System.Drawing.Image?

c# - 测试依赖 MEF 注入(inject)的非导出类

wpf - 绑定(bind)到 CompositeCollection 中的单个元素