WPF TreeviewItem宽度

时间:2017-11-14 15:43:17

标签: c# wpf treeviewitem

我试图阻止我的treeviewitems溢出窗口大小时 内容太长了。我尝试了各种方法(绑定ParentWidth of parent,Horizo​​ntalContentAlign,Wrapping into other containers,...)但没有成功。

我想实现两件事:

  • 所有TreeviewItems必须填充剩余空间(Stretch)
  • 我不想要一个水平滚动条,而是我想要的文本 文本框溢出。

到目前为止,这是我的代码。

任何想法??

KR Manu

enter image description here

 <TreeView x:Name="tv">
        <TreeView.Resources>
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="KeyboardNavigation.TabNavigation" Value="Continue"/>
                <Setter Property="IsTabStop" Value="True"/>
                <Setter Property="Focusable" Value="False" />
                <Setter Property="IsExpanded" Value="True" />
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="HorizontalAlignment" Value="Stretch"/>
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                <Setter Property="Padding" Value="1,0,0,1"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TreeViewItem}">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition MinWidth="19" Width="Auto"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition/>
                                </Grid.RowDefinitions>

                                <!-- Connecting Lines -->
                                <Rectangle x:Name="HorLn" Margin="9,1,0,0" Height="1" Stroke="Red" SnapsToDevicePixels="True"/>
                                <Rectangle x:Name="VerLn" Width="1" Stroke="Red" Margin="0,0,1,0" Grid.RowSpan="2" SnapsToDevicePixels="true"/>
                                <ToggleButton Margin="-1,0,0,0" x:Name="Expander" Visibility="Hidden"  IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press"/>
                                <Border x:Name="Bd" Grid.Column="1" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
                                    <ContentPresenter Grid.Column="1" x:Name="PART_Header" ContentSource="Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" MinWidth="20"/>
                                </Border>
                                <ItemsPresenter x:Name="ItemsHost" Grid.Row="1" Grid.Column="1"/>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsExpanded" Value="false">
                                    <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed"/>
                                </Trigger>
                                <Trigger Property="HasItems" Value="false">
                                    <Setter TargetName="Expander" Property="Visibility" Value="Hidden"/>
                                </Trigger>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="HasHeader" Value="false"/>
                                        <Condition Property="Width" Value="Auto"/>
                                    </MultiTrigger.Conditions>
                                    <Setter TargetName="PART_Header" Property="MinWidth" Value="75"/>
                                </MultiTrigger>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="HasHeader" Value="false"/>
                                        <Condition Property="Height" Value="Auto"/>
                                    </MultiTrigger.Conditions>
                                    <Setter TargetName="PART_Header" Property="MinHeight" Value="19"/>
                                </MultiTrigger>
                                <Trigger Property="IsSelected" Value="true">
                                    <Setter TargetName="Bd" Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                                </Trigger>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="IsSelected" Value="true"/>
                                        <Condition Property="IsSelectionActive" Value="false"/>
                                    </MultiTrigger.Conditions>
                                    <Setter TargetName="Bd" Property="Background" Value="Transparent"/>
                                    <Setter Property="Foreground" Value="White"/>
                                </MultiTrigger>
                                <Trigger Property="IsEnabled" Value="false">
                                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </TreeView.Resources>
        <TreeView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <TextBox Text="{Binding Mode=OneWay}" />
                </Grid>
            </DataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

2 个答案:

答案 0 :(得分:0)

也许,你有两个选择。

TextBox TextTrimming

或者您将在文本框中设置char的最大长度。

答案 1 :(得分:0)

不幸的是Birke的答案并没有解决问题。 我修改了我的TreeviewItem模板:

  <TreeView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Border BorderThickness="1" BorderBrush="Red">
                        <TextBlock TextTrimming="CharacterEllipsis" Text="{Binding Mode=OneWay}" />
                    </Border>
                </Grid>
            </DataTemplate>
        </TreeView.ItemTemplate>

物品仍然在窗口宽度上溢出。