【问题标题】:WPF TreeView with Icon remove selected on the left of the iconWPF TreeView 图标左侧选中了图标删除
【发布时间】:2016-06-22 07:52:12
【问题描述】:

我有一个 WPF TreeView,我想删除图标左侧的蓝色“选定”悬停。 我希望你明白我想要什么:D

这是问题的图片

TreeView 由后面的代码填充,我以编程方式构建项目。

TreeViewItem.Header XAML 如下所示:

<StackPanel Orientation="Horizontal" ...>
    <Image ... />
    <TextBlock .../>
</StackPanel>

【问题讨论】:

    标签: c# wpf xaml treeview


    【解决方案1】:

    这就像一种变通方法,但不是很好的解决方案:

    <StackPanel Orientation="Horizontal" Margin="-1,0,0,0">  
       <Image Source="/Images/yourImage.jpg" Height="30" Width="30"/>
       <TextBlock Text="Hey"/>
    </StackPanel>
    

    不过,最好使用HierarchicalDataTemplate

    <TreeView  ItemsSource="{Binding Leafs}">                                
      <TreeView.Resources>                    
        <HierarchicalDataTemplate DataType="{x:Type vm:LeafViewModel}" ItemsSource="{Binding Children}">
           <ContentControl Content="{Binding }">
             <ContentControl.Style>
               <Style TargetType="{x:Type ContentControl}">
                  <Setter Property="ContentTemplate" Value="{StaticResource DefaultTemplate}"/>
               </Style>
             </ContentControl.Style>
           </ContentControl>                        
       </HierarchicalDataTemplate>
     </TreeView.Resources>            
    </TreeView>
    

    及其HierarchicalDataTemplate

    <HierarchicalDataTemplate x:Key="DefaultTemplate" DataType="{x:Type vm:LeafViewModel}" ItemsSource="{Binding Children}">
      <Border Tag="{Binding DataContext, RelativeSource={RelativeSource Self}}" Background="Transparent">
         <StackPanel Orientation="Horizontal">
            <Label VerticalAlignment="Center" FontFamily="WingDings" Content="1"/>
            <CheckBox IsChecked="{Binding IsCheckedFoo}"/>
            <TextBlock Name="leafTxtBox" Text="{Binding LeafName}" Tag="{Binding DataContext, RelativeSource={RelativeSource Self}}" Background="Transparent"/>
         </StackPanel>
      </Border>
    </HierarchicalDataTemplate>
    

    关于如何使用TreeView without violating MVVM rules is by Josh Smith.的最佳文章

    【讨论】:

    • 我不太适合 MVVM 和 DataBinding。你的第一个例子对我来说是正确的......我知道,我必须学习 xaml :D
    【解决方案2】:

    TreeView 将项目包装到 TreeViewItem 容器中。 TreeViewItem 有 Padding 1,0,0,0 可以通过 Style 移除

    <TreeView.Resources>
        <Style TargetType="TreeViewItem">
            <Setter Property="Padding" Value="0"/>
        </Style>
    </TreeView.Resources>
    

    【讨论】:

      【解决方案3】:

      一般有4个画笔对应这个,被TreeViewItem的默认Template使用

      键:

      HighlightBrushKey - 带焦点的背景。

      HighlightTextBrushKey - 具有焦点的前景。

      InactiveSelectionHighlightBrushKey - 没有焦点的背景。

      InactiveSelectionHighlightTextBrushKey - 没有焦点的前景。

      只要你认为合适就覆盖它们,对于你的要求,这样的事情就可以了:

      <TreeView>
        <TreeView.Resources>
          <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
                            Color="Transparent" />
          <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}"
                            Color="Black" />
          <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}"
                            Color="Transparent" />
          <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}"
                            Color="Black" />
        </TreeView.Resources>
      </TreeView>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-19
        • 2020-07-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多