那个小三角是 TreeViewItem 中的一个部件,
名为 ExpanderButton ,类型为 ToggleButton ,功能:用于展开 TreeView 控件的 ToggleButton。
 
很遗憾 TreeViewItem 似乎没有单独把设置这个ExpanderButton的Style公开出来,
所以要改就只能为整个 TreeViewItem 指定Style了。

更改为:

  <ToggleButton x:Name="ExpanderButton"  IsTabStop="False" TabNavigation="Once" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <ToggleButton.Template>
                <ControlTemplate TargetType="ToggleButton">
                    <Grid x:Name="Root" Background="Transparent">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <!--<VisualState x:Name="MouseOver">
                                    <Storyboard>                                        
                                        <ColorAnimation Duration="0" Storyboard.TargetName="UncheckedVisual" Storyboard.TargetProperty="(Path.Stroke).Color" To="#FF1BBBFA"/>
                                    </Storyboard>
                                </VisualState>-->
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0" Storyboard.TargetName="Root" Storyboard.TargetProperty="Opacity" To=".7"/>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="CheckStates">
                                <VisualState x:Name="Unchecked">
<Storyboard>
                                        <DoubleAnimation Duration="0" Storyboard.TargetName="CheckedVisual" Storyboard.TargetProperty="Opacity" To="1"/>
                                        <DoubleAnimation Duration="0" Storyboard.TargetName="UncheckedVisual" Storyboard.TargetProperty="Opacity" To="0"/>
                                    </Storyboard>
</
VisualState>

<VisualState x:Name="Checked"> <Storyboard> <DoubleAnimation Duration="0" Storyboard.TargetName="UncheckedVisual" Storyboard.TargetProperty="Opacity" To="1"/> <DoubleAnimation Duration="0" Storyboard.TargetName="CheckedVisual" Storyboard.TargetProperty="Opacity" To="0"/> </Storyboard> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <Grid HorizontalAlignment="Right" Margin="2 2 5 2"> <Image x:Name="UncheckedVisual" Opacity="0" Source="a.jpg" ></Image> <Image x:Name="CheckedVisual" Opacity="1" Source="b.jpg" ></Image> <!--<Path x:Name="UncheckedVisual" Fill="#FFFFFFFF" Stroke="#FF989898" StrokeLineJoin="Miter" StrokeThickness="1" Height="9" HorizontalAlignment="Right" VerticalAlignment="Center" Width="6" Data="M 0,0 L 0,9 L 5,4.5 Z"/> <Path x:Name="CheckedVisual" Fill="#FF262626" StrokeLineJoin="Miter" Height="6" HorizontalAlignment="Center" VerticalAlignment="Center" Width="6" Opacity="0" Data="M 6,0 L 6,6 L 0,6 Z"/>--> </Grid> </Grid> </ControlTemplate> </ToggleButton.Template> </ToggleButton>
具体的操作方式:

Changing Visual States – Changing the Collapsed and Extended Visual States


Looking at this last Print screen a bit more closely we can see It uses to types of Icons:


 把silverlight treeview 节点前面的小三角换成自定义的图片  - Expanded Icon


把silverlight treeview 节点前面的小三角换成自定义的图片  - Collapsed Icon


We’d like to change those to:


把silverlight treeview 节点前面的小三角换成自定义的图片 - Collapsed Icon


把silverlight treeview 节点前面的小三角换成自定义的图片  - Expanded Icon


 


In order to do that, we’ll have to edit the Template of the TreeViewItem generated by the TreeView.


To do that, we’ll change the TreeView ItemContainerStyle that gets applied on to each generated TreeViewItem.


We’ll select the TreeView, go to “Object –> Edit Other Styles –> Edit ItemContainerStyle –> Edit Copy”.


把silverlight treeview 节点前面的小三角换成自定义的图片


And we’ll call the new Style “AlienItemStyle”.


把silverlight treeview 节点前面的小三角换成自定义的图片


Next, we’ll need to drill into editing the template for the TreeViewItems.


We’ll do that through “Edit Template –> Edit Controls Parts (Template) –> Edit Template”.


把silverlight treeview 节点前面的小三角换成自定义的图片


 


Here’s what we see:


把silverlight treeview 节点前面的小三角换成自定义的图片


 


There are a few VisualStateManager states here. And we’ve got a template that has a few visual elements in it.


One of those is the Expander button.


把silverlight treeview 节点前面的小三角换成自定义的图片


 


In order to change the TreeViewItem Icons we’ll need to edit the Template for the ExpanderButotn.


Right Click on the ExpanderButton –> Edit Template –> Edit Template.


把silverlight treeview 节点前面的小三角换成自定义的图片


 


And here’s what we see:


把silverlight treeview 节点前面的小三角换成自定义的图片


 


Apparently, the TreeViewItem ExpanderButton has two visuals: “CheckedVisual” 把silverlight treeview 节点前面的小三角换成自定义的图片  and “UncheckedVisual” 把silverlight treeview 节点前面的小三角换成自定义的图片 .


We’ll need to replace those with our new Visuals.


 


First, I’ll draw a whole ellipse.


把silverlight treeview 节点前面的小三角换成自定义的图片


And on top of it I’ll draw a rectangle.


把silverlight treeview 节点前面的小三角换成自定义的图片


I’ll select the Ellipse with the Rectangle. Right Click –> Combine –> Subtract.


把silverlight treeview 节点前面的小三角换成自定义的图片


And we’ll get this path:


把silverlight treeview 节点前面的小三角换成自定义的图片


I’ll repeat the process and we’ll also get this path:


把silverlight treeview 节点前面的小三角换成自定义的图片


 


Now, In XAML we’ll cut & paste the names of “CheckedVisual” and “UncheckedVisual” to these new elements.


把silverlight treeview 节点前面的小三角换成自定义的图片


becomes


把silverlight treeview 节点前面的小三角换成自定义的图片


 


Next, we’ll delete the old CheckVisual and UncheckedVisual and place our new ones into the correct position.


把silverlight treeview 节点前面的小三角换成自定义的图片


 


Now, one last thing we have to change before this runs, is making sure the “UncheckedVisual” is hidden during the “Checked” state.


We’ll go the the “Checked” state.


把silverlight treeview 节点前面的小三角换成自定义的图片


Select the “UncheckedVisual” and set it’s opacity to 0.


把silverlight treeview 节点前面的小三角换成自定义的图片


 


Now if we run our sample:


把silverlight treeview 节点前面的小三角换成自定义的图片



 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-25
  • 2021-10-30
  • 2021-11-04
  • 2022-12-23
猜你喜欢
  • 2022-01-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-13
  • 2022-12-23
相关资源
相似解决方案