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

本文介绍如何修改TreeViewItem中的图标样式,通过编辑TreeViewItem的模板来自定义展开和折叠状态的图标,包括选择新的图标样式并调整其在不同状态下的显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

那个小三角是 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:


 image  - Expanded Icon


image  - Collapsed Icon


We’d like to change those to:


image - Collapsed Icon


image  - 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”.


image


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


image


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”.


image


 


Here’s what we see:


image


 


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.


image


 


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.


image


 


And here’s what we see:


image


 


Apparently, the TreeViewItem ExpanderButton has two visuals: “CheckedVisual” image  and “UncheckedVisual” image .


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


 


First, I’ll draw a whole ellipse.


image


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


image


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


image


And we’ll get this path:


image


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


image


 


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


image


becomes


image


 


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


image


 


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.


image


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


image


 


Now if we run our sample:


image



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值