使用网格分析器自动调整树视图的宽度

时间:2009-07-29 13:53:02

标签: silverlight treeview gridsplitter

我有一个带有网格的silverlight3.0应用程序。 XAML在下面。 当用户更改左列的宽度时,我想让TreeView控件调整大小并包装内容。目前树视图显示滚动条而不是包装。 在右栏中,这样可以正常工作,因为没有树视图但是堆栈面板。

当我使用HierarchicalDataTemplate而不是静态treeviewitems时,会发生同样的情况。

关于如何使这项工作的任何想法?

谢谢, -cc

  <Grid x:Name="LayoutRoot" Background="Aqua">


            <Grid.RowDefinitions>
                <RowDefinition />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition  />
            </Grid.ColumnDefinitions>
        <StackPanel Orientation="Vertical" Grid.Column="0"  Background="Yellow">
            <controls:TreeView  >

                <controls:TreeViewItem Header="test1 some text some text some text some text some text">
                    <controls:TreeViewItem Header="test1.1 some text some text some text some text some text">
                    </controls:TreeViewItem>
                </controls:TreeViewItem>
                <controls:TreeViewItem    Header="test2 some text some text some text some text some text some text some text some text some text some text some text">
                    <controls:TreeViewItem Header="test2 some text some text some text some text some text some text some text some text some text some text some text">
                    </controls:TreeViewItem>

                </controls:TreeViewItem>
            </controls:TreeView>
        </StackPanel>
        <controls:GridSplitter Grid.Row="0" Grid.Column="1" Width="5" VerticalAlignment="Stretch" 
                                       HorizontalAlignment="Center" Background="AliceBlue"/>
        <StackPanel Grid.Column="2" Orientation="Vertical">
            <TextBlock TextWrapping="Wrap"> dwd kwdh wkd wkd wkd wkjd hwkdjhw kdw dkw k</TextBlock>
            <TextBlock TextWrapping="Wrap"> dwd kwdh wkd wkd wkd wkjd hwkdjdwd kwdh wkd wkd wkd wkjd hwkdjhw kdw dkw k</TextBlock>
        </StackPanel>
    </Grid>

1 个答案:

答案 0 :(得分:0)

该死;我来到这里寻找这个问题的答案。

我能做的最好的事情是将TextBlock的MaxWidth绑定到TreeView的大小,并提供偏移量。尝试将此XAML用于(WPF)TreeView,并摆脱封闭的StackPanel:

    <TreeView>
        <TreeViewItem>

            <TreeViewItem.Header>
                <TextBlock
                    Text="Maecenas praesent accumsan bibendum dictumst eleifend facilisi faucibus habitant inceptos interdum lobortis nascetur pharetra placerat "
                    Background="Azure"
                    TextWrapping="Wrap"
                    MaxWidth="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TreeView}}, Path=ActualWidth, Converter={StaticResource ValueSubtractor}, ConverterParameter=30}"
                />
            </TreeViewItem.Header>

            <TextBlock
                Text="Faucibus habitant inceptos interdum lobortis nascetur pharetra placerat pulvinar sagittis senectus sociosqu suscipit torquent ultrices"
                Background="#FFBDBDE6"
                TextWrapping="Wrap"
                MaxWidth="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TreeView}}, Path=ActualWidth, Converter={StaticResource ValueSubtractor}, ConverterParameter=50}"
            />

            <TextBlock
                Text="Placerat pulvinar sagittis senectus sociosqu suscipit torquent ultrices vehicula volutpat maecenas praesent accumsan bibendum dictumst"
                Background="#FF9090E0"
                TextWrapping="Wrap"
                MaxWidth="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TreeView}}, Path=ActualWidth, Converter={StaticResource ValueSubtractor}, ConverterParameter=50}"
            />

        </TreeViewItem>
    </TreeView>

您需要编写一个执行偏移的值转换器,并确保树的每个级别的偏移都足够大,以便在滑动分割器时不会出现水平滚动条。

我对这种技术的一个问题是,当我在HiearchicalDataTemplate上尝试使用相同的MaxWidth绑定时,Blend 3抱怨XAML无效,我得到红色波浪形。它仍然运行,但我期待Blend的XAML解析器更新!