如何在 wpf 中为 TreeView 进行文本换行 [英] How to make text wrap for a TreeView in wpf

查看:27
本文介绍了如何在 wpf 中为 TreeView 进行文本换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了一天的大部分时间试图弄清楚这一点,我知道问题在于 TextBlock 认为它具有无限宽度,因此从未发生换行,我尝试了很多绑定来设置宽度网格、内容窗格等;没有任何运气;这个用户控件托管在一个可以动态调整大小的更大应用程序的 ig:Dock 中,所以我不能只为文本设置宽度,我相信我需要某种绑定......任何帮助都是赞赏.

I have spent most of the day trying to figure this out, I know that the problem is that the TextBlock thinks that it has infinite width, so the wrapping never occurs, I have tried a lot of bindings to set the width of the grid, ContentPane, etc; without any luck; this user control is hosted withing a ig:Dock of a larger application that can be dynamically re-sized, so I can't just set a width to the text, I believe I need some sort of binding... any help would be appreciated.

<UserControl x:Class="VistaSTAR_Client.Views.XMLEditor"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:igDock="http://infragistics.com/DockManager"
        xmlns:ig="http://schemas.infragistics.com/xaml"
        mc:Ignorable="d" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">

    <UserControl.Resources>
        <SolidColorBrush x:Key="xmlValueBrush" Color="Blue" />
        <SolidColorBrush x:Key="xmAttributeBrush" Color="Red" />
        <SolidColorBrush x:Key="xmlTagBrush" Color="DarkMagenta" />
        <SolidColorBrush x:Key="xmlMarkBrush" Color="Blue" />
        <DataTemplate x:Key="attributeTemplate">
            <StackPanel Orientation="Horizontal" Margin="3,0,0,0" HorizontalAlignment="Center">
                <TextBlock Text="{Binding Path=Name}" Foreground="{StaticResource xmAttributeBrush}"/>
                <TextBlock Text="=&quot;" Foreground="{StaticResource xmlMarkBrush}"/>
                <TextBlock Text="{Binding Path=Value}" Foreground="{StaticResource xmlValueBrush}"/>
                <TextBlock Text="&quot;" Foreground="{StaticResource xmlMarkBrush}"/>
            </StackPanel>
        </DataTemplate>

        <HierarchicalDataTemplate x:Key="NodeTemplate">
            <StackPanel Orientation="Horizontal" Focusable="False">
                <TextBlock x:Name="tbName" Text="?" />
                <ItemsControl ItemTemplate="{StaticResource attributeTemplate}" ItemsSource="{Binding Path=Attributes}" HorizontalAlignment="Center">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal"/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                </ItemsControl>
            </StackPanel>
            <HierarchicalDataTemplate.ItemsSource>
                <Binding XPath="child::node()" />
            </HierarchicalDataTemplate.ItemsSource>
            <HierarchicalDataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=NodeType}" Value="Text">
                    <Setter TargetName="tbName" Property="Text" Value="{Binding Path=Value}"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
                    <Setter TargetName="tbName" Property="Text" Value="{Binding Path=Name}"/>
                </DataTrigger>
            </HierarchicalDataTemplate.Triggers>
        </HierarchicalDataTemplate>
        <XmlDataProvider x:Key="xmlDataProvider">
        </XmlDataProvider>
    </UserControl.Resources>



    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <igDock:XamDockManager Name="XMLDockManager" Theme="IGTheme" LayoutMode="FillContainer">
            <igDock:XamDockManager.Panes>
                <igDock:SplitPane Width="Auto" HorizontalAlignment="Stretch">
                    <igDock:TabGroupPane Width="Auto" HorizontalAlignment="Stretch">
                    <igDock:ContentPane Name="XMLTreeView" Header="Tree View" Width="Auto" HorizontalAlignment="Stretch">
                        <TreeView Name="treeView1" Background="AliceBlue" HorizontalAlignment="Stretch" Height="Auto" Width="Auto" Margin="5,5,5,5"
                            ItemsSource="{Binding Source={StaticResource xmlDataProvider}, XPath=*}" ItemTemplate= "{StaticResource NodeTemplate}"/>
                    </igDock:ContentPane>
                    <igDock:ContentPane Name="XML" Header="Editor">
                        <TextBox HorizontalAlignment="Stretch" Name="text" Text=" " TextWrapping="WrapWithOverflow" Height="Auto" Width="Auto"
                            VerticalScrollBarVisibility="Auto" VerticalAlignment="Stretch" AcceptsReturn="True" AcceptsTab="True" Margin="5,5,5,5" Grid.Row="1" />
                    </igDock:ContentPane>
                    </igDock:TabGroupPane>
                </igDock:SplitPane>
            </igDock:XamDockManager.Panes>
        </igDock:XamDockManager>

    </Grid>
</UserControl>

推荐答案

好的,这里可能有几个可能导致您的文本不换行的原因.

Ok, here may be several things that may cause your text do not wrap.

  1. 如果你想在 TextBoxTextBlock 中包裹文本,你需要设置 TextWrapping="Wrap",
  2. 您必须使用 WrapPanel 而不是 StackPanelStack Panel 从它拥有的项目中获取他的宽度.
  3. 这是最重要的,每个树视图在 ScrollViewer 中都有内容,默认情况下 TreeViewScrollViewer 给它的项目由于启用了水平栏,无限宽度,如果您想要包装文本和项目,则需要禁用此功能 (ScrollViewer.Horizo​​ntalScrollBarVisibility="Disabled").
  1. If you want a text inside a TextBox or TextBlock be wrapped you need to set TextWrapping="Wrap",
  2. You must use a WrapPanel and not a StackPanel, a Stack Panel take his width from the items it have.
  3. And this is most important, each tree view has the content inside a ScrollViewer, by default the TreeView's ScrollViewer give its items Infinity width due has the Horizantal bar enabled, you need to disable this if you want the text and items be wrapped (ScrollViewer.HorizontalScrollBarVisibility="Disabled").

希望能帮到你...

这篇关于如何在 wpf 中为 TreeView 进行文本换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆