1

したがって、次のスタイルを持つ TreeViewItem があります。

                   <Style TargetType="{x:Type TreeViewItem}">
                        <Setter Property="HeaderTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <Image Name="img" Width="20" Height="16" Stretch="Uniform" Source="Images/Folder.png"/>
                                        <TextBlock Text="{Binding}" Margin="5,0" />
                                    </StackPanel>
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>

選択すると、TextBlock AND Image が強調表示されます。TextBlock を強調表示して、ファイル エクスプローラーのフォルダー ツリーのように機能させようとしています。

4

2 に答える 2

1

私はこの問題に対する(ややハッキーな)が軽量の解決策を見つけました。この質問はかなり古いようですが、それでも、他の人が見つけられるように、ここに解決策を投稿します。

TreeViewでは、選択が変更されたときにTreeViewItemの背景を設定するために使用される2つのブラシをオーバーライドします。また、ブラシのコピーを作成して、後でデータテンプレートで復元できるようにします。

<TreeView ItemsSource="{Binding Path=SomeDataSource}">
    <TreeView.Resources>
         <SolidColorBrush x:Key="_CustomHighlightBrushKey" Color="{Binding Source={StaticResource {x:Static SystemColors.HighlightBrushKey}}, Path=Color}" />
         <SolidColorBrush x:Key="_CustomControlBrushKey" Color="{Binding Source={StaticResource {x:Static SystemColors.ControlBrushKey}}, Path=Color}" />
         <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
         <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
    </TreeView.Resources>
</TreeView>

これをDynamicResourceバインディングで機能させることができなかったため、このソリューションはおそらくテーマの変更では機能しないことに注意してください。これを行う方法があるかどうかを知りたいと思います。

次に、ツリーノードのフォーマットに次の階層データテンプレートを使用します。

<HierarchicalDataTemplate DataType="{x:Type SomeViewModelType}" ItemsSource="{Binding Path=Children}">
    <StackPanel Orientation="Horizontal">
        <StackPanel.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="{Binding Source={StaticResource {x:Static SystemColors.HighlightBrushKey}}, Path=Color}" />
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{Binding Source={StaticResource {x:Static SystemColors.ControlBrushKey}}, Path=Color}" />
        </StackPanel.Resources>
        <Image SnapsToDevicePixels="True" Source="...">
        </Image>
        <TextBlock Text="{Binding Path=DisplayName}" Margin="5,0">
            <TextBlock.Resources>
                <Style TargetType="{x:Type TextBlock}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}}, Path=IsSelected}" Value="True">
                            <Setter Property="Background" Value="{DynamicResource _CustomHighlightBrushKey}" />
                        </DataTrigger>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}}, Path=IsSelected}" Value="True" />
                                <Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}}, Path=IsSelectionActive}" Value="False" />
                            </MultiDataTrigger.Conditions>
                            <Setter Property="Background" Value="{DynamicResource _CustomControlBrushKey}" />
                        </MultiDataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Resources>
        </TextBlock>
    </StackPanel>
</HierarchicalDataTemplate>

コンテキストメニューを正しくレンダリングできるように、システムブラシを元の(静的)値に復元することに注意してください。

于 2011-10-23T11:10:44.250 に答える
0

独自のハイライト マークをロールアップする必要があるため、コントロールがパネルの背景全体を青く塗りつぶすのではなく、TreeViewItem.IsSelected が True の場合のトリガーに基づいて独自のハイライト フォーマットを設定します。

あなたの場合、これはテキストコンテナの背景を青(設定すると通常は白)に設定し、画像コンテナの背景を白のままにし、コンテナの背景全体を白に設定します。

メソッドはここで説明されています:リンクテキスト

于 2009-11-09T22:00:41.917 に答える