2013-03-05 87 views
0

我被要求创建基于SDK的基本TreeView的自定义TreeView。这一切都有效,问题是:节点/树叶不能伸展到它们的内容,它们的宽度始终相同。这里是XAML标记:Silverlight TreeViewItem宽度变化

<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'宽‘设置自动’,在风格上设置标记ItemContainer宽度。没有任何工作,TreeViewItems总是相同的宽度,它太狭窄。

编辑:是的,SDK的命名空间添加到网格中,刚好发生在编辑器上,它没有显示,无法帮助它。

Edit2:TreeView实际上将其宽度更改为给定值,但其项目仍未缩放。

回答

0

尝试定义网格行或列并将高度/宽度设置为“*”,以便它可以使用尽可能多的空间。

<Grid.RowDefintions> 
    <RowDefintion Height="*" /> 
</Grid.RowDefintions> 

<Grid.ColumnDefinitons> 
    <ColumnDefinition Width="*" /> 
</Grid.ColumnDefinitions> 
+0

这不是它;/ – Rufix 2013-03-05 11:29:35

+0

试试你的内部“SDK:HierarchicalDataTemplate”使用网格,然后在它里面一个TextBlock。并将该网格中的RowDefintion和ColumnDefiniton设置为“*” – hijack 2013-03-05 11:57:47

+0

仍然不是。要尝试ScaleToContent的东西。 – Rufix 2013-03-05 12:18:43