【发布时间】:2013-05-04 12:14:53
【问题描述】:
我是 WPF 表单的新手,在尝试在 TreeViewItem 中设置背景时遇到了一个问题。
<Window x:Class="wpf_test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<!--Styles-->
<Style x:Key="GUIEntity" TargetType="{x:Type Control}">
<Setter Property="MinWidth" Value="150" />
<Setter Property="MaxWidth" Value="150" />
</Style>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="True" />
</Style>
<!--Data Sources-->
<x:Array x:Key="BooleanListData" Type="sys:String" xmlns:sys="clr-namespace:System;assembly=mscorlib">
<sys:String>True</sys:String>
<sys:String>False</sys:String>
</x:Array>
</Window.Resources>
<Grid>
<TreeView Name="treeView1" Margin="5" Background="Azure">
<TreeViewItem Header="ComplexTypeProperty" Margin="5">
<CheckBox Style="{StaticResource GUIEntity}" Margin="3,3,10,3" Content="Instance" />
<StackPanel Orientation="Horizontal" Background="LightGray">
<Label Margin="0,2,0,0" Content="IsBoolProperty" Width="150" />
<ComboBox Margin="5" Style="{StaticResource GUIEntity}" ItemsSource="{StaticResource BooleanListData}" />
</StackPanel>
</TreeViewItem>
</TreeView>
</Grid>
</Window>
问题是在 StackPanel 中设置的背景没有达到 TreeView 控件的全宽(向右)。我尝试将 HorizontalAllignment="Stretch" 添加到 TreeView 向下的所有控件,但没有效果。 StackPanel 上的背景宽度仅到 ComboBox 的末尾。
通过在 TreeView 上设置背景,我确认它确实占据了表单的整个宽度,所以这不是问题。
有谁知道如何将背景扩展到 TreeView 大小的末尾?
我将如何以最简单的方式覆盖此网格?
【问题讨论】:
-
我猜这篇文章涵盖了你的问题:stackoverflow.com/questions/15826637/…'code'
-
@GeorgySmirnov 只是在标题上设置背景。它似乎对其他任何东西都没有影响。并且标题的背景仅延伸到文本而不是全宽。 (我也不希望宽度向左延伸,只向右延伸)。
-
据我所见,我想我想在突出显示的网格上做一个
Grid.ColumnSpan="2",这会做我想做的事。但是我该怎么做呢?