文字换行、网格和星形调整大小
我有一些文本数据
,我想在Grid
中显示,其中包含三列,中间一列的宽度是其他两列的两倍,占据了网格。 文本
很长,需要换行。我无法开始工作(从过去的其他查询中,我看到其他人也遇到过类似的问题)是让自动换行和调整网格大小来工作。我所拥有的是:
<Window.Resources>
<local:DTData x:Key="dtData" />
</Window.Resources>
<StackPanel DataContext="{StaticResource dtData}">
<ListBox ItemsSource="{Binding}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Border x:Name="a" Grid.Column="0" Margin="4"/>
<TextBlock Margin="4" Grid.Column="0" TextWrapping="Wrap"
Text="{Binding A}" Width="{Binding ActualWidth, ElementName=a }" MinWidth="100"/>
<Border x:Name="b" Grid.Column="1" Margin="4"/>
<TextBlock Margin="4" Grid.Column="1" TextWrapping="Wrap"
Text="{Binding B}" Width="{Binding ActualWidth, ElementName=b }" MinWidth="100"/>
<Border x:Name="c" Grid.Column="2" Margin="4"/>
<TextBlock Margin="4" Grid.Column="2" TextWrapping="Wrap"
Text="{Binding C}" Width="{Binding ActualWidth, ElementName=b }" MinWidth="100"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
这使用了 WPF TextBox 和 Scroll 中的 Border
技巧行为强制文本换行工作,但列的宽度是设置的最小宽度或最长的单词(如果更大)。
有谁知道如何强制列适合网格的宽度?
I have some text data
that I want to display in a Grid
, with three columns, the middle column being twice as wide as the other two, taking up the full width of the grid. The text
is long and needs to be wrapped. What I can't get to work (and from other queries here in the past, I see others have had similar problems) is getting word wrap and sizing to the grid to work. What I have is:
<Window.Resources>
<local:DTData x:Key="dtData" />
</Window.Resources>
<StackPanel DataContext="{StaticResource dtData}">
<ListBox ItemsSource="{Binding}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Border x:Name="a" Grid.Column="0" Margin="4"/>
<TextBlock Margin="4" Grid.Column="0" TextWrapping="Wrap"
Text="{Binding A}" Width="{Binding ActualWidth, ElementName=a }" MinWidth="100"/>
<Border x:Name="b" Grid.Column="1" Margin="4"/>
<TextBlock Margin="4" Grid.Column="1" TextWrapping="Wrap"
Text="{Binding B}" Width="{Binding ActualWidth, ElementName=b }" MinWidth="100"/>
<Border x:Name="c" Grid.Column="2" Margin="4"/>
<TextBlock Margin="4" Grid.Column="2" TextWrapping="Wrap"
Text="{Binding C}" Width="{Binding ActualWidth, ElementName=b }" MinWidth="100"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
This uses the Border
trick from WPF TextBox and Scroll behavior to force text wrapping to work but the column's width is either the minimum width set or the longest word if greater.
Does anyone know of a way to force the columns to fit the width of the grid?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确定要自动换行吗?或者您的目标是文本修剪?我这样问是因为当我在 kaxaml 中输入以下 xaml 时,中间列中的文本被完美包裹:
如果要修剪文本,只需将
TextWrapping="NoWrap"
和 TextTrimming 设置为 < code>TextTrimming="CharacterEllipsis" 在需要的地方。也可能您没有提供所有数据来重现您所描述的问题......
You sure you wanna wrap the text? Or you goal is text trimming? I'm asking because when I type the following xaml in kaxaml, the text in the middle column is perfectly wrapped:
If you want to trim the text, just set
TextWrapping="NoWrap"
and TextTrimming toTextTrimming="CharacterEllipsis"
where needed.There might be also that you haven't provided all the data, to reproduce the problem you are describing...