我被要求基于SDK中的基本TreeView创建自定义TreeView。一切正常,问题是:节点/叶子不会拉伸到它们的内容,它们的宽度总是相同的。这是xaml标记:
<Grid xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' xmlns:sdk='http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk'>
<Grid.Resources>
<Style x:Key='TreeViewChildStyle' TargetType='sdk:TreeViewItem'>
<Setter Property='HorizontalContentAlignment' Value='Stretch'/>
<Setter Property='Background' Value='Blue'/>
</Style>
<sdk:HierarchicalDataTemplate x:Key='ChildTemplate' x:Name='ChildTemplate'>
<TextBlock HorizontalAlignment='Stretch' Text='{Binding Path=ChildPath}'/>
</sdk:HierarchicalDataTemplate>
<sdk:HierarchicalDataTemplate x:Key='NameTemplate' ItemsSource='{Binding Path= ChildrenCollectionPath}' x:Name='NameTemplate' ItemTemplate='{StaticResource ChildTemplate}'>
<TextBlock HorizontalAlignment='Stretch' Text='{Binding Path=ParentPath}' Width='1000'/>
</sdk:HierarchicalDataTemplate>
</Grid.Resources>
<sdk:TreeView Height='Auto' Background='Red' ItemContainerStyle='{StaticResource TreeViewChildStyle}' Name='treeView1' ItemTemplate='{StaticResource NameTemplate} VerticalAlignment='Stretch' HorizontalAlignment='Stretch' BorderThickness='0'/>
</Grid>
我已经尝试在后面的代码中设置ItemContainer宽度,将TextBlocks的宽度设置为'Auto',在样式标记中设置ItemContainer宽度。没有任何作用,TreeViewItems总是相同的宽度,它太窄了。
编辑:是的,在网格中添加了SDK命名空间,编辑器发生了一些事情并且未显示,无法帮助它。
Edit2:TreeView实际上将其宽度更改为给定值,但其Items仍然没有缩放。
答案 0 :(得分:0)
尝试定义网格行或列并将高度/宽度设置为“*”,以便它可以使用尽可能多的空间。
<Grid.RowDefintions>
<RowDefintion Height="*" />
</Grid.RowDefintions>
<Grid.ColumnDefinitons>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>