C# 如何在列表框模板中对齐TextBlock=Stretch?
我正在尝试创建一个列表框模板(用于项目)。这就是我得到的C# 如何在列表框模板中对齐TextBlock=Stretch?,c#,wpf,templates,xaml,styles,C#,Wpf,Templates,Xaml,Styles,我正在尝试创建一个列表框模板(用于项目)。这就是我得到的 <Window.Resources> <DataTemplate x:Key="ItemTemplate"> <Grid Margin="5"> <Grid.ColumnDefinitions> <ColumnDefinition Width="60&quo
<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>
这是它看起来的样子
你能看到文本块的宽度增加了吗?我想显示列表框中的字符串换行。我做错了什么
<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" ...>
我认为您需要设置文本块的宽度-否则它将增长以适应内容。您尝试过文本块的宽度=自动吗?Guau,它工作正常。还有一个问题,您知道如何对齐堆栈面板内的所有文本块(我指的是Strength)。您当前设置了
HorizontalAlignment=“Center”
位于下方的TextBlock
上,应设置为Stretch
。
<TextBlock TextWrapping="Wrap" ...>