使用 gridsplitter 自动调整树视图的宽度

发布于 2024-07-29 00:32:02 字数 2217 浏览 9 评论 0原文

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

当我使用 HierarchicalDataTemplate 而不是静态树视图项时,也会发生同样的情况。

关于如何实现这项工作有什么想法吗?

谢谢, -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>

I have a silverlight3.0 aplication with a Grid. The XAML is below.
I'd like to have the TreeView control resize and content wrapped when the user changes the width of the left column. Currently the treeview displays a scrollbar instead of wrapping.
On the right column this works fine as there isno treeview but stackpanel.

The same happens when I use a HierarchicalDataTemplate instead of static treeviewitems.

Any ideas on how to make this work?

Thanks,
-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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缱倦旧时光 2024-08-05 00:32:02

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

我能做的就是将 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 解析器更新!

Damn; I landed here looking for an answer for just that question.

Best I can do is bind the MaxWidth of the TextBlock to the size of the TreeView, and provide an offset. Try this XAML for a (WPF) TreeView, and get rid of that enclosing 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>

You'll need to write a value converter that performs the offset, and make sure the offsets for each level of the tree are suffieciently large so that the horizontal scrollbar doesn't appear when your sliding the splitter around.

One issue I have with this technique is when I try this same binding for the MaxWidth on a HiearchicalDataTemplate, Blend 3 complains that the XAML is invalid and I get red squigglies. It still runs of course, but I look forward to a XAML parser update for Blend!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文