2

我花了一天的大部分时间试图弄清楚这一点,我知道问题是 TextBlock 认为它具有无限宽度,所以包装永远不会发生,我尝试了很多绑定来设置网格的宽度,内容窗格等;没有任何运气;这个用户控件托管在一个更大的应用程序的 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>
4

2 回答 2

2

好的,这里可能有几件事可能导致您的文本不换行。

  1. 如果您想在 a 中包含文本TextBox或被TextBlock包装,则需要设置TextWrapping="Wrap"
  2. 您必须使用 WrapPanel 而不是StackPanel,Stack Panel从它拥有的项目中获取他的宽度。
  3. 这是最重要的,每个树视图在 a 中都有内容ScrollViewer,默认情况下,给它TreeView的项目无限宽度由于启用了水平栏,如果你想要文本和项目被包装( )ScrollViewer,你需要禁用它。ScrollViewer.HorizontalScrollBarVisibility="Disabled"

希望这可以帮助你...

于 2012-11-09T16:22:26.360 回答
0
<TextBlock Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ENTERTYPE}, Path=ActualWidth}"/>

ENTERTYPE应该控制TextBlock宽度的祖先的类型在哪里。

即如果TextBlock包含在 aStackPanel中,也许 ENTERTYPE=StackPanel

于 2012-11-09T02:26:44.253 回答