如何在wpf中为TreeView进行文本换行

时间:2012-11-08 21:28:19

标签: c# wpf xaml treeview

我花了大部分时间试图解决这个问题,我知道问题是TextBlock认为它有无限宽度,所以包装永远不会发生,我尝试了很多绑定来设置宽度网格,ContentPane等;没有运气;这个用户控件是用一个ig:托管一个更大的应用程序的Dock,可以动态调整大小,所以我不能只设置文本的宽度,我相信我需要某种绑定...任何帮助将是赞赏。

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

2 个答案:

答案 0 :(得分:2)

好的,这里可能有几件事可能导致你的文字没有换行。

  1. 如果您希望将TextBoxTextBlock内的文字包裹起来,则需要设置TextWrapping="Wrap"
  2. 你必须使用WrapPanel而不是StackPanelStack Panel从他拥有的物品中取出他的宽度。
  3. 这是最重要的,每个树视图都有ScrollViewer内的内容,默认情况下TreeView的{​​{1}}给它的项目无限宽度到期,启用了Horizantal栏,如果您希望包装文本和项目(ScrollViewer)。
  4. ,则需要禁用此功能

    希望这可以帮助你...

答案 1 :(得分:0)

<TextBlock Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ENTERTYPE}, Path=ActualWidth}"/>

ENTERTYPE是应控制TextBlock宽度的祖先类型。

即。如果TextBlock包含在StackPanel中,可能是ENTERTYPE = StackPanel