【问题标题】:WPF TreeView with right alignment values具有右对齐值的 WPF TreeView
【发布时间】:2011-06-25 13:27:20
【问题描述】:

我的 wpf 树视图有问题。它的代码是这样的:

<TreeView ItemsSource="{Binding Path=Items}" Grid.RowSpan="2" Grid.ColumnSpan="2">
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type m:MyTreeItem}" ItemsSource="{Binding Items}">
                <DockPanel LastChildFill="True">
                    <TextBlock Text="{Binding Path=Value}" DockPanel.Dock="Right"/>
                    <TextBlock Text="{Binding Path=Display}" DockPanel.Dock="Left"/>
                </DockPanel>
            </HierarchicalDataTemplate>
        </TreeView.Resources>
    </TreeView>

其中 MyTreeItem 是一个简单的类,具有 2 个字符串属性(显示和值)和一个名为 Items 的 MyTreeItem 列表。

我需要树视图来显示所有向右对齐的“值”,同时根据深度维护项目“显示”的树列表。

我尝试将dockPanel设置为固定宽度,但没有成功。

我想在没有任何 c# 代码的情况下执行此操作,只需 xaml。

提前致谢。

【问题讨论】:

    标签: wpf treeview alignment


    【解决方案1】:

    如果我正确理解您的问题,您想拉伸TreeViewItems 并将Value 向右对齐。要使TreeViewItems 横向拉伸,您需要编辑TreeViewItem 的模板。问题在这里解释:http://leecampbell.blogspot.com/2009/01/horizontal-stretch-on-treeviewitems.html

    这是TreeViewItem 的编辑版本,它执行此操作。请注意,您必须添加对 PresentationFramework.Aero 的引用才能使其工作

    示例

    <TreeView ItemsSource="{Binding Path=Items}"
                Grid.RowSpan="2"
                Grid.ColumnSpan="2"
                ItemContainerStyle="{StaticResource StretchTreeViewItemStyle}">
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type m:MyTreeItem}" ItemsSource="{Binding Items}">
                <DockPanel LastChildFill="True">
                    <TextBlock Text="{Binding Path=Value}" DockPanel.Dock="Right"/>
                    <TextBlock Text="{Binding Path=Display}" DockPanel.Dock="Left"/>
                </DockPanel>
            </HierarchicalDataTemplate>
        </TreeView.Resources>
    </TreeView>
    

    StretchTreeViewItemStyle

    <Style x:Key="TreeViewItemFocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Rectangle/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <PathGeometry x:Key="TreeArrow" Figures="M0,0 L0,6 L6,0 z"/>
    <Style x:Key="ExpandCollapseToggleStyle" TargetType="{x:Type ToggleButton}">
        <Setter Property="Focusable" Value="False"/>
        <Setter Property="Width" Value="16"/>
        <Setter Property="Height" Value="16"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                    <Border Background="Transparent" Height="16" Padding="5,5,5,5" Width="16">
                        <Path x:Name="ExpandPath" Data="{StaticResource TreeArrow}" Fill="Transparent" Stroke="#FF989898">
                            <Path.RenderTransform>
                                <RotateTransform Angle="135" CenterY="3" CenterX="3"/>
                            </Path.RenderTransform>
                        </Path>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Stroke" TargetName="ExpandPath" Value="#FF1BBBFA"/>
                            <Setter Property="Fill" TargetName="ExpandPath" Value="Transparent"/>
                        </Trigger>
                        <Trigger Property="IsChecked" Value="True">
                            <Setter Property="RenderTransform" TargetName="ExpandPath">
                                <Setter.Value>
                                    <RotateTransform Angle="180" CenterY="3" CenterX="3"/>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="Fill" TargetName="ExpandPath" Value="#FF595959"/>
                            <Setter Property="Stroke" TargetName="ExpandPath" Value="#FF262626"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="StretchTreeViewItemStyle"
        TargetType="{x:Type TreeViewItem}"
        xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
        <Setter Property="Padding" Value="1,0,0,0"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="FocusVisualStyle" Value="{StaticResource TreeViewItemFocusVisual}"/>
        <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>
                        <ToggleButton x:Name="Expander" ClickMode="Press" IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ExpandCollapseToggleStyle}"/>
                        <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="1" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                            <ContentPresenter x:Name="PART_Header" ContentSource="Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </Border>
                        <ItemsPresenter x:Name="ItemsHost" Grid.Column="1" Grid.Row="1"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsExpanded" Value="false">
                            <Setter Property="Visibility" TargetName="ItemsHost" Value="Collapsed"/>
                        </Trigger>
                        <Trigger Property="HasItems" Value="false">
                            <Setter Property="Visibility" TargetName="Expander" Value="Hidden"/>
                        </Trigger>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter Property="Background" TargetName="Bd" 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 Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                        </MultiTrigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="VirtualizingStackPanel.IsVirtualizing" Value="true">
                <Setter Property="ItemsPanel">
                    <Setter.Value>
                        <ItemsPanelTemplate>
                            <VirtualizingStackPanel/>
                        </ItemsPanelTemplate>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
    

    【讨论】:

    • 谢谢!这就是我要找的东西!
    • 链接失效
    【解决方案2】:
    class StretchedTreeView : TreeView {
        protected override void OnItemContainerStyleChanged(Style oldItemContainerStyle, Style newItemContainerStyle) {
            base.OnItemContainerStyleChanged(oldItemContainerStyle, newItemContainerStyle);
            newItemContainerStyle.Setters
                .Add(new EventSetter(LoadedEvent, new RoutedEventHandler(TreeViewItem_Loaded)));
        }
        void TreeViewItem_Loaded(object sender, RoutedEventArgs e) {
            TreeViewItem item = sender as TreeViewItem;
            if (item == null) {
                return;
            }
            ContentPresenter cp = FindVisualChild<ContentPresenter>(item);
            cp.HorizontalAlignment = HorizontalAlignment.Stretch;
            Border border = FindVisualChild<Border>(item, "Bd");
            border.SetValue(Grid.ColumnSpanProperty, 2);
        }
        static T FindVisualChild<T>(DependencyObject obj, string name = null) where T : DependencyObject {
            for (int i = 0 ; i < VisualTreeHelper.GetChildrenCount(obj) ; i++) {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is T
                    && (name == null || !(child is FrameworkElement) || name == ((FrameworkElement) child).Name)) {
                    return (T) child;
                }
                else {
                    T childOfChild = FindVisualChild<T>(child, name);
                    if (childOfChild != null) {
                        return childOfChild;
                    }
                }
            }
            return null;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-02
      • 2016-11-15
      • 2013-08-23
      • 2012-09-12
      • 1970-01-01
      • 2011-10-09
      • 1970-01-01
      相关资源
      最近更新 更多