Wpf 将边界延伸到现有空间
我有这个:Wpf 将边界延伸到现有空间,wpf,xaml,Wpf,Xaml,我有这个: <Border Background="Gray"> <TextBlock x:Name="Text" Text="{Binding Name}" Margin="0, 5" FontSize="16"/> </Border> 看起来是这样的:(有三个) 我希望它看起来像这样: <Setter Property="ContentTempla
<Border Background="Gray">
<TextBlock x:Name="Text"
Text="{Binding Name}"
Margin="0, 5"
FontSize="16"/>
</Border>
看起来是这样的:(有三个)
我希望它看起来像这样:
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Border>
<TextBlock x:Name="Text"
Text="{Binding Name}"
Margin="0, 5"
FontSize="16"/>
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
(边界延伸到空间的末端+对边界高度的一些控制。)
p、 我不必使用边界,任何能达到同样效果的东西都可以。
更新:这是ListBoxItem的数据模板的一部分。它的定义样式如下:
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Border>
<TextBlock x:Name="Text"
Text="{Binding Name}"
Margin="0, 5"
FontSize="16"/>
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
我试图将水平对齐设置为“拉伸”,但没有成功。有什么想法吗?A
StackPanel
如果您的TextBlock
号码是固定的:
<StackPanel Grid.Column="1">
<StackPanel.Resources>
<Style x:Key="style1" TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="0,5" />
<Setter Property="FontSize" Value="16" />
<Setter Property="Background" Value="Gray" />
<Setter Property="Foreground" Value="White" />
</Style>
</StackPanel.Resources>
<TextBlock Text="Text 1" Style="{StaticResource style1}" />
<TextBlock Text="Text 2" Style="{StaticResource style1}" />
<TextBlock Text="Text 3" Style="{StaticResource style1}" />
</StackPanel>
如果您的
TextBlock
号码固定,则StackPanel
将起作用:
<StackPanel Grid.Column="1">
<StackPanel.Resources>
<Style x:Key="style1" TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="0,5" />
<Setter Property="FontSize" Value="16" />
<Setter Property="Background" Value="Gray" />
<Setter Property="Foreground" Value="White" />
</Style>
</StackPanel.Resources>
<TextBlock Text="Text 1" Style="{StaticResource style1}" />
<TextBlock Text="Text 2" Style="{StaticResource style1}" />
<TextBlock Text="Text 3" Style="{StaticResource style1}" />
</StackPanel>
最简单的方法是使用网格行。 以下是一个例子:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="10"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="10"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="text 1" Background="gray"/>
<TextBlock Grid.Row="2" Text="text 2" Background="gray"/>
<TextBlock Grid.Row="4" Text="text 3" Background="gray"/>
</Grid>
最简单的方法是使用网格行。 以下是一个例子:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="10"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="10"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="text 1" Background="gray"/>
<TextBlock Grid.Row="2" Text="text 2" Background="gray"/>
<TextBlock Grid.Row="4" Text="text 3" Background="gray"/>
</Grid>
试试这个
<Border Background="Gray" HorizontalAlignment="Stretch">
<TextBlock x:Name="Text"
HorizontalAlignment="Left"
Text="{Binding Name}"
Margin="0, 5"
FontSize="16"/>
</Border>
试试这个
<Border Background="Gray" HorizontalAlignment="Stretch">
<TextBlock x:Name="Text"
HorizontalAlignment="Left"
Text="{Binding Name}"
Margin="0, 5"
FontSize="16"/>
</Border>
最后,我发现这是实现我想要的最简单的解决方案:
- 我创建了一个网格(一列一行)
- 我在里面创建了一个矩形(它会自动延伸到网格空间)
- 我创建了一个文本框(它会自动在矩形的顶部呈现)
<Grid>
<Rectangle x:Name="fillColor" Fill="..."/>
<TextBox ... />
</Grid>
最后,我发现这是实现我想要的最简单的解决方案:
- 我创建了一个网格(一列一行)
- 我在里面创建了一个矩形(它会自动延伸到网格空间)
- 我创建了一个文本框(它会自动在矩形的顶部呈现)
<Grid>
<Rectangle x:Name="fillColor" Fill="..."/>
<TextBox ... />
</Grid>
HorizontalAlignment=“Stretch”(在边框上)可以显示更多代码吗?边框放在哪里?在ListBox的ItemTemplate中?很好奇为什么不为TextBox设置背景?@FoggyFinder,因为可能我想要的不仅仅是文本。HorizontalAlignment=“Stretch”(在边框上)可以显示更多代码吗?边框放在哪里?在ListBox的ItemTemplate中?只是好奇为什么不为TextBox设置背景?@FoggyFinder,因为可能我想要的不仅仅是文本。为什么这比使用StackPanel更容易?因为网格行将自动调整为窗口宽度,StackPanel不会。尝试一下,你会看到神奇的发生。@Nawed Nabi Zada,是的,你是对的,在这种简单的情况下,垂直堆叠面板也可以工作。这完全是个人喜好。我倾向于不使用StackPanel,因为方向方向超限。为什么这比使用StackPanel更容易?因为网格行将自动调整为窗口宽度,StackPanel不会。尝试一下,你会看到神奇的发生。@Nawed Nabi Zada,是的,你是对的,在这个简单的情况下,垂直StackPanel也可以工作。这完全是个人喜好。我倾向于不使用stackpanels,因为方向超限。