C# 树视图中的背景项不是全宽
我是WPF表单的新手,在尝试在TreeViewItem中设置背景时遇到了一个问题C# 树视图中的背景项不是全宽,c#,wpf,xaml,C#,Wpf,Xaml,我是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" Heig
<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控件的全宽(向右)。我尝试将horizontallallignment=“Stretch”
添加到树视图下的所有控件中,但没有效果。StackPanel上的背景宽度仅延伸到组合框的末端
通过在树视图上设置背景,我确认它确实占据了表单的整个宽度,所以这不会成为问题
有人知道如何将背景延伸到树视图大小的末端吗
如何以最简单的方式覆盖此网格?以下是一篇博客文章,介绍了这个问题并给出了解决方案。就我记忆所及,它对我来说还不错。基本上,您需要重新部署TreeViewItem。很多Xaml,但我认为这是唯一合适的解决方案
这只是用一些视觉效果来扩展这篇老文章 问题声明:默认情况下无法右对齐TreeView中的内容 -基于这里和那里的一些其他评论-我测试过从Xaml中删除任何StackPanel(但这不是问题,因为我可以使用stackpanels在TreeView之外复制所需的设计) TLDR:解决方法已在以前的评论中发布 XAML:
<Window x:Class="Test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DbSeeder.WPF.View"
mc:Ignorable="d"
Title="Test"
Height="450"
Width="800"
Loaded="Window_Loaded">
<DockPanel>
<!-- what i want -->
<StackPanel DockPanel.Dock="Top"
Margin="5">
<Label Content="This is the expected structure"
FontFamily="Verdana"
FontWeight="Bold"
BorderBrush="Black"
BorderThickness="2"
HorizontalAlignment="Stretch"
Background="Orange"
/>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<!-- Buttons -->
<StackPanel Grid.Column="1"
Orientation="Horizontal"
HorizontalAlignment="Right"
Margin="3">
<Button Content="Button 1"
Margin="5"/>
<Button Content="Button 2"
Margin="3"/>
</StackPanel>
<!-- Keys -->
<StackPanel Grid.Column="0"
Orientation="Horizontal"
HorizontalAlignment="Left"
Margin="3" >
<Label Content="Here should come key 1" Margin="3" BorderBrush="Black" BorderThickness="2"/>
<Label Content="Here should come key 2" Margin="3" BorderBrush="Black" BorderThickness="2"/>
</StackPanel>
</Grid>
</StackPanel>
<!-- What I get 2-->
<Grid
ShowGridLines="True"
DockPanel.Dock="Top"
Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Content="This is what I get"
FontFamily="Verdana"
FontWeight="Bold"
BorderBrush="Black"
BorderThickness="2"
Background="Orange"
Grid.Column="0"
Grid.Row="0"/>
<TreeView x:Name="alternative2"
Grid.Column="0"
Grid.Row="1"
HorizontalContentAlignment="Stretch"
HorizontalAlignment="Stretch">
<TreeView.Resources>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<Grid ShowGridLines="True"
DockPanel.Dock="Top"
Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<!-- Buttons -->
<Button Content="Button 1"
Grid.Column="2"
Margin="5"
HorizontalAlignment="Stretch"/>
<Button Content="Button 2"
Grid.Column="3"
Margin="3"
HorizontalAlignment="Stretch"/>
<!-- Keys -->
<Label Grid.Column="0"
HorizontalAlignment="Stretch"
Content="Here should come key 1"
Margin="3"
BorderBrush="Black"
BorderThickness="2"/>
<Label Grid.Column="1"
HorizontalAlignment="Stretch"
Content="Here should come key 2"
Margin="3"
BorderBrush="Black"
BorderThickness="2"/>
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</TreeView.Resources>
</TreeView>
</Grid>
</DockPanel>
以及隐藏的代码(为视图添加种子)
公共部分类测试:窗口
{
受保护的IDictionary JsonFields{get;set;}
公开考试()
{
初始化组件();
var x=新字典()
{
{“键1”,“字符串”},
{“键2”,“字符串2”}
};
var y=新列表()
{
“次级地级5A”,
“次级列表5b”,
};
JsonFields=newdictionary()
{
{“键1”,“字符串”},
{“键2”,“字符串”},
{“键3”,“字符串”},
{“键4-dict”,x},
{“键5-列表”,y}
};
}
已加载私有无效窗口(对象发送器、路由目标)
{
var计数器=0;
foreach(JsonFields.Keys中的var键)
{
计数器++;
var item=new TreeViewItem()
{
标题=键,
ClipToBounds=true
};
如果(计数器%2==0)
{
var子项=新树视图项()
{
Header=$“{item.Header}的子键”,
ClipToBounds=true
};
项目.项目.添加(子项目);
}
备选方案2.项目。添加(项目);
}
}
}
我想这篇文章涵盖了你的问题:“代码”@GeorgySmirnov,它只是在标题上设置背景。这对其他任何事情都没有影响。标题的背景只延伸到文本,而不是全宽。(另外,我不想让宽度向左延伸,只想向右延伸)。从我看到的情况来看,我想在高亮显示的网格上做一个
网格。ColumnSpan=“2”
,这将满足我的需要。但是我该怎么做呢?你有没有试着将TreeViewItem样式上的HorizontalContentAlignment设置为“Stretch”?是的。并将其设置到StackPanel。很难说。。您可以尝试使用Snoop检查visualTree。也许这会给你答案是什么导致了固定的宽度。我也会尝试使用网格而不是StackPanel,并放置三列,第一列Width=“Auto”用于标签,第二列Width=“Auto”用于标签,第三列Width=“*”用于ComboboxTested网格现在正在签出snoop。snoop实际上什么都不做。我确认最新的下载是boken。
public partial class Test : Window
{
protected IDictionary<string, object> JsonFields { get; set; }
public Test()
{
InitializeComponent();
var x = new Dictionary<string, string>()
{
{"key 1", "string" },
{"key 2", "string 2" }
};
var y = new List<string>()
{
"subList5a",
"subList5b",
};
JsonFields = new Dictionary<string, object>()
{
{ "Key 1", "string" },
{ "Key 2", "string" },
{ "Key 3", "string" },
{ "Key 4 - dict", x },
{ "Key 5 - List", y }
};
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var counter = 0;
foreach (var key in JsonFields.Keys)
{
counter++;
var item = new TreeViewItem()
{
Header = key,
ClipToBounds = true
};
if (counter % 2 == 0)
{
var subItem = new TreeViewItem()
{
Header = $"SubKey of {item.Header}",
ClipToBounds = true
};
item.Items.Add(subItem);
}
alternative2.Items.Add(item);
}
}
}