文字换行、网格和星形调整大小

发布于 2024-08-12 08:31:45 字数 1806 浏览 4 评论 0原文

我有一些文本数据,我想在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 技术交流群。

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

发布评论

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

评论(1

悸初 2024-08-19 08:31:45

您确定要自动换行吗?或者您的目标是文本修剪?我这样问是因为当我在 kaxaml 中输入以下 xaml 时,中间列中的文本被完美包裹:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Grid>  
    <StackPanel>
  <ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" Width="350">
    <ListBox.ItemTemplate>
      <DataTemplate>
        <Grid>
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="*" />
          </Grid.ColumnDefinitions>
          <TextBlock Margin="4" Grid.Column="0" TextWrapping="Wrap" 
                     Text="Binding A"  MinWidth="100"/>
          <TextBlock Margin="4" Grid.Column="1" TextWrapping="Wrap" 
                     Text=" Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco"  MinWidth="100"/>
          <TextBlock Margin="4" Grid.Column="2" TextWrapping="Wrap" 
                     Text="Binding C" MinWidth="100"/>
        </Grid>
      </DataTemplate>
    </ListBox.ItemTemplate>
    <TextBlock Text="" />
    <TextBlock Text=""/>
    <TextBlock Text=""/>
  </ListBox>
  </StackPanel>
  </Grid>
</Page>

如果要修剪文本,只需将 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:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Grid>  
    <StackPanel>
  <ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" Width="350">
    <ListBox.ItemTemplate>
      <DataTemplate>
        <Grid>
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="*" />
          </Grid.ColumnDefinitions>
          <TextBlock Margin="4" Grid.Column="0" TextWrapping="Wrap" 
                     Text="Binding A"  MinWidth="100"/>
          <TextBlock Margin="4" Grid.Column="1" TextWrapping="Wrap" 
                     Text=" Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco"  MinWidth="100"/>
          <TextBlock Margin="4" Grid.Column="2" TextWrapping="Wrap" 
                     Text="Binding C" MinWidth="100"/>
        </Grid>
      </DataTemplate>
    </ListBox.ItemTemplate>
    <TextBlock Text="" />
    <TextBlock Text=""/>
    <TextBlock Text=""/>
  </ListBox>
  </StackPanel>
  </Grid>
</Page>

If you want to trim the text, just set TextWrapping="NoWrap" and TextTrimming to TextTrimming="CharacterEllipsis" where needed.

There might be also that you haven't provided all the data, to reproduce the problem you are describing...

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