Robert
Robert

Reputation: 6226

Programmatically Setting ControlTemplate Item

I'm having trouble figuring out how to programmatically setting the "Stroke" of my arrow. I'm using these buttons in a menu bar and I want the one currently selected to have it's arrow change to green and all the others gray.

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

Upvotes: 0

Views: 826

Answers (2)

John Bowen
John Bowen

Reputation: 24453

You generally want to express something that is a state being expressed visually as a property on the control. Button doesn't have a built in property to express a selected state, but ToggleButton does: IsChecked. If you can't use a ToggleButton you could create an Attached Property that can be set from your code that's determining the currently selected Button. That could then be used in a Trigger in the same way you would use IsChecked:

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

Upvotes: 1

Goblin
Goblin

Reputation: 8022

Can't you add a trigger:

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

Or is it the Path not updating with your triggers that is the problem?

Upvotes: 0

Related Questions