【问题标题】:Silverlight TreeViewItem width changeSilverlight TreeViewItem 宽度变化
【发布时间】:2013-03-05 19:37:53
【问题描述】:

我被要求根据 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 仍未缩放。

【问题讨论】:

    标签: silverlight xaml treeview treeviewitem


    【解决方案1】:

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

    <Grid.RowDefintions>
         <RowDefintion Height="*" />
    </Grid.RowDefintions>
    
    <Grid.ColumnDefinitons>
         <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    

    【讨论】:

    • 尝试在“sdk:HierarchicalDataTemplate”中使用 Grid,然后在其中使用 TextBlock。并将该 Grid 中的 RowDefintion 和 ColumnDefiniton 设置为“*”
    • 还是不行。将尝试使用 ScaleToContent 的东西。
    猜你喜欢
    • 2018-04-27
    • 1970-01-01
    • 1970-01-01
    • 2015-04-14
    • 2018-04-25
    • 1970-01-01
    • 1970-01-01
    • 2011-05-05
    • 2012-04-30
    相关资源
    最近更新 更多