【问题标题】:How to align TextBlock = Stretch in a listbox template?如何在列表框模板中对齐 TextBlock = Stretch?
【发布时间】:2012-02-13 22:10:53
【问题描述】:

我正在尝试创建一个列表框模板(用于项目)。这就是我所拥有的。

<Window.Resources>
    <DataTemplate x:Key="ItemTemplate">
        <Grid Margin="5">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <StackPanel Grid.Column="0">
                <TextBlock Text="TEKS" FontSize="20" FontWeight="Bold" Background="#FF502F8F"  Foreground="White" Width="{Binding Path=Width, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type StackPanel}}}" TextAlignment="Center" />
                <TextBlock Text="{Binding Level}" FontSize="24" Background="#FF058C44" HorizontalAlignment="Center" Width="{Binding Path=Width, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type StackPanel}}}" Foreground="White" TextAlignment="Center" FontFamily="Segoe UI Light" />
            </StackPanel>
            <TextBlock Grid.Column="1" Text="{Binding Owner}" FontSize="20" FontWeight="Bold" TextWrapping="Wrap" />
        </Grid>
    </DataTemplate>
</Window.Resources>

<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource SampleDataSource}}">
    <ListBox ItemTemplate="{DynamicResource ItemTemplate}" ItemsSource="{Binding Collection}" Margin="44,39,82,103" Style="{DynamicResource ListBoxStyle}"/>
</Grid>

这是它的样子。

你能看到文本块扩展得更多吗?我想在列表框中显示字符串换行。我做错了什么?

【问题讨论】:

  • 我认为您需要设置文本块的宽度 - 否则它将增长以适应内容。
  • 你尝试过 width=auto 的文本块吗?

标签: c# wpf templates xaml styles


【解决方案1】:
  1. 禁用水平滚动:

    <ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" ...>
    
  2. 启用文本换行(已经如此):

    <TextBlock TextWrapping="Wrap" ...>
    

【讨论】:

  • Guau,它适用于 H.B.还有一个问题,你知道如何对齐堆栈面板内的所有文本块(我的意思是拉伸)。
  • 您当前将HorizontalAlignment="Center" 设置在应设置为Stretch 的较低TextBlock 上。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-30
  • 2020-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多