TreeViewItem中的背景不是全角

问题描述:

我是WPF表单的新手,当尝试在TreeViewItem中设置背景时遇到问题。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控件的全部宽度(向右)。我尝试从TreeView向下添加HorizontalAllignment="Stretch"到所有控件,但它没有效果。 StackPanel上的背景宽度只能到ComboBox的末尾。

通过在TreeView上设置背景,我确认它确实占用了表单的整个宽度,所以不会是问题。

有谁知道如何将背景延伸到TreeView大小的末尾?

我怎么会去最简单的方式重写此格?

+0

我想这个职位涵盖了你的问题: http://stackoverflow.com/questions/15826637/wpf-treeview-item-background-over-整个排 '代码' 2013-05-04 12:38:11

+0

@GeorgySmirnov,只是在头上设置背景。它接缝对其他任何东西都没有影响。标题的背景只能扩展到文本而不是全部宽度。 (我也不希望宽度延伸到左侧,只有右侧)。 – 2013-05-04 12:45:45

+0

从我看到的,我想我会想要做一个'Grid.ColumnSpan =“2”'在突出显示的网格上,这将做我想做的。但我该怎么做? – 2013-05-05 00:17:18

这是一篇博客文章,涵盖问题并提供解决方案。据我记得,它对我来说确实很好。基本上你需要重新模板TreeViewItem。 XAML代码很多,但我认为是唯一的妥善解决

https://leecampbell.com/2009/01/14/horizontal-stretch-on-treeviewitems/

+0

无法使其正常工作。 – 2013-05-04 12:52:00

+1

您是否尝试将TreeViewItem样式的Horizo​​ntalContentAlignment设置为“Stretch”? – jure 2013-05-04 12:56:09

+0

是。并将其设置为StackPanel。 – 2013-05-04 12:58:12