对WPF很新颖。如何防止TreeView的DataTemplate导致控件水平展开?
<TreeView Grid.Column="0" x:Name="DataTree" MinWidth="175" BorderThickness="0">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type self:DispatchTreeTerritoryItem}" ItemsSource="{Binding ClientLocations}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" />
</StackPanel>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type self:DispatchTreeClientLocationItem}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding ClientName,StringFormat='({0}) '}" />
<TextBlock Text="{Binding Address}" />
</StackPanel>
</DataTemplate>
</TreeView.Resources>
</TreeView>
在DispatchTreeClientlocationItem
性质(ClientName
和Address
)的数据,不要在树视图的宽度内配合。所以当创建这些项目时,treeview控件会自行水平扩展以允许所有树节点的文本可见。
这是不希望的行为。我希望它的行为像WinForms TreeView那样,文本只是在控件的边缘被截断。这在WPF中可能吗?如何?
UPDATE
继@芬克的评论和链接的问题,建议我的TreeView XAML现在看起来像:
<TreeView x:Name="DataTree" Grid.Column="0" MinWidth="175" BorderThickness="0"
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
我观察到的行为没有任何变化。当节点文本太长以至于不能适应TreeView的水平范围时,TreeView会水平展开以显示所有文本。
这里有几个截图来说明问题。展开领域节点时,子节点的内容会使TreeView水平展开。
你可以通过在“TreeView”上设置ScrollViewer.HorizontalScrollBarVisibility =“Disabled”禁用扩展。 Ref [this](https://stackoverflow.com/questions/45776693/making-a-portion-of-a-treeviewitem-wrap)文章,进一步向前迈进。 – Funk
@Funk:按照你的建议没有产生可观察的变化。 –
@SamAxe,TreeView位于哪个容器?如果Grid和columnDefinition具有'Width =“Auto”',则扩展是预期的行为。尝试设置最大宽度或比例宽度 – ASh