重用TreeView的展开[+]和塌陷[ - ]在WPF按钮 [英] Reusing a TreeView's expand [+] and collapse [-] buttons in WPF

查看:269
本文介绍了重用TreeView的展开[+]和塌陷[ - ]在WPF按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法重用简单的扩大 [+] 和崩溃 [ - ]出现一个按钮在一个WPF TreeView控件节点?我想在我的展开和折叠一些控件的应用程序在其他地方也有类似的图形。

Is there a way to reuse the simple expand [+] and collapse [-] buttons that appear next to nodes in a WPF TreeView? I would like to have a similar graphic elsewhere in my application for expanding and collapsing some controls.

推荐答案

它实际上是一个切换按钮,我检查了的 SimpleStyles 项目,这是我发现的:

It's actually a ToggleButton, I checked the TreeView template on the SimpleStyles project and this is what I found:

    <ControlTemplate TargetType="ToggleButton">
      <Grid
        Width="15"
        Height="13"
        Background="Transparent">
        <Path x:Name="ExpandPath"
          HorizontalAlignment="Left" 
          VerticalAlignment="Center" 
          Margin="1,1,1,1"
          Fill="{StaticResource GlyphBrush}"
          Data="M 4 0 L 8 4 L 4 8 Z"/>
      </Grid>
      <ControlTemplate.Triggers>
        <Trigger Property="IsChecked"
             Value="True">
          <Setter Property="Data"
              TargetName="ExpandPath"
              Value="M 0 4 L 8 4 L 4 8 Z"/>
        </Trigger>
      </ControlTemplate.Triggers>
    </ControlTemplate>

所以这就是你需要做的,使其工作是什么:

So this is what you need to do to make it work:

<Window x:Class="StackOverflowTests.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" x:Name="window1" Height="300" Width="300"
 Loaded="window1_Loaded"
 xmlns:local="clr-namespace:StackOverflowTests">
 <Window.Resources>
  <SolidColorBrush x:Key="GlyphBrush" Color="#444" />
  <ControlTemplate x:Key="toggleButtonTemplate" TargetType="ToggleButton">
   <Grid
            Width="15"
            Height="13"
            Background="Transparent">
    <Path x:Name="ExpandPath"
              HorizontalAlignment="Left" 
              VerticalAlignment="Center" 
              Margin="1,1,1,1"
              Fill="{StaticResource GlyphBrush}"
              Data="M 4 0 L 8 4 L 4 8 Z"/>
   </Grid>
   <ControlTemplate.Triggers>
    <Trigger Property="IsChecked"
                 Value="True">
     <Setter Property="Data"
                  TargetName="ExpandPath"
                  Value="M 0 4 L 8 4 L 4 8 Z"/>
    </Trigger>
   </ControlTemplate.Triggers>
  </ControlTemplate>
  <Style x:Key="toggleButtonStyle" TargetType="ToggleButton">
   <Setter Property="Template" Value="{StaticResource toggleButtonTemplate}" />
  </Style>
 </Window.Resources>
 <StackPanel>
  <ToggleButton x:Name="toggleButton" Height="20" Width="20" Style="{StaticResource toggleButtonStyle}" />
 </StackPanel>
</Window>


  • 首先,你把模板(toggleButtonTemplate),并把它放在你的资源

  • 然后你做一个样式(toggleButtonStyle),设置控件的模板(toggleButtonTemplate)

  • 最后你告诉你的切换按钮的风格是toggleButtonStyle

  • 如果你只是把它复制粘贴应该直出的工作。

    If you just copy paste from to it should straight out work.

    这是一个简单的过程,但它可以给你一个头痛,如果你不习惯使用模板,让我知道如果你有任何问题。

    It's a simple process but it can give you a headache if you're not used to using templates, let me know if you have any questions.

    要了解一点的路径迷你语言:

    To learn a little about the paths mini-language:

    几何迷你语言

    这篇关于重用TreeView的展开[+]和塌陷[ - ]在WPF按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆