以编程方式设置ControlTemplate项

时间:2010-06-06 20:38:48

标签: c# wpf

我无法弄清楚如何以编程方式设置箭头的“笔划”。我在菜单栏中使用这些按钮,我希望当前选中的按钮将箭头更改为绿色,其他所有箭头变为灰色。

    <Style x:Key="FooterGrayButtonStyle" TargetType="{x:Type Button}">  
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="MinHeight" Value="35" />
    <Setter Property="FontSize" Value="16"/>
    <Setter Property="Margin" Value="0,5,10,5" />
    <Setter Property="Foreground" Value="White" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <Border x:Name="Bd" Background="#FF535A65" BorderBrush="Black" BorderThickness="1" CornerRadius="10">
                        <Path x:Name="arrow" Stretch="Fill" Stroke="#FFB1BB1C" StrokeThickness="5" HorizontalAlignment="Right" Margin="5" 
                                StrokeEndLineCap="Round" StrokeStartLineCap="Round" StrokeLineJoin="Miter" Width="13" Height="23" Data="M0,0 L1,1 0,2" />
                    </Border>
                    <ContentPresenter HorizontalAlignment="Left" Margin="15,-2,30,0" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter TargetName="Bd" Property="Background" Value="#FFB1BB1C" />
                        <Setter Property="Stroke" TargetName="arrow" Value="White"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Stroke" TargetName="arrow" Value="#FFB7B7B7"/>
                        <Setter Property="Foreground" Value="#FFB7B7B7"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

2 个答案:

答案 0 :(得分:1)

您通常希望表达一种状态,即视觉上表示为控件上的属性。 Button没有内置属性来表示选定状态,但ToggleButton执行:IsChecked。如果你不能使用ToggleButton,你可以创建一个Attached Property,可以从你的代码中设置,确定当前选择的Button。然后可以像使用IsChecked一样在Trigger中使用它:

<Trigger Property="local:MyProperties.IsSelected" Value="True">
    <Setter TargetName="arrow" Property="Stroke" Value="Green"/>
</Trigger>

答案 1 :(得分:0)

无法添加触发器:

<Trigger Property="IsFocused" Value="True">
    <Setter Property="StrokeEndLineCap" Value="Arrow" TargetName="arrow"/>
</Trigger>

或者是路径没有用你的触发器更新是问题吗?