Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
First and foremost, I apologize for my grammatical errors; my first language is Persian (Iran).
I have a button that I changed its appearance and I wanting to change color of lines that is inside it with Color Animation.but this error was displaying: "'LineA' name cannot be found in the name scope of 'Libraries.SettingsWindow'"

What I have tried:

XAML
<Window.Resources>
    <Style x:Key="CustomButtonStyle" TargetType="{x:Type Button}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border x:Name="Border" Cursor="Hand" BorderBrush="DarkGray" BorderThickness="0.5" CornerRadius="9" Background="WhiteSmoke">
                            <StackPanel Margin="0,0,0,0" Height="24" Width="24">
                                <Line x:Name="LineA" Stroke="Green" StrokeThickness="2" StrokeStartLineCap="Round" StrokeEndLineCap="Round" X1="4" X2="20" Y1="9" Y2="9"/>
                                <Line x:Name="LineB" Stroke="White" StrokeThickness="2" StrokeStartLineCap="Round" StrokeEndLineCap="Round" X1="4" X2="20" Y1="2" Y2="2"/>
                                <Line x:Name="LineC" Stroke="Red" StrokeThickness="2" StrokeStartLineCap="Round" StrokeEndLineCap="Round" X1="4" X2="20" Y1="2" Y2="2"/>
                            </StackPanel>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
            <Storyboard x:Key="CustomButtonLineAIsCheckedTrue">
            <ColorAnimation From="{x:Null}" To="Green" Duration="00:00:01" Storyboard.TargetName="LineA" SpeedRatio="4" AutoReverse="False"/>
        </Storyboard>
        <Storyboard x:Key="CustomButtonLineAIsCheckedFalse">
            <ColorAnimation From="Green" To="{x:Null}" Duration="00:00:01" Storyboard.TargetName="LineA" SpeedRatio="4" AutoReverse="False"/>
        </Storyboard>
        <Storyboard x:Key="CustomButtonLineBIsCheckedTrue">
            <ColorAnimation From="{x:Null}" To="White" Duration="00:00:01" Storyboard.TargetName="LineB" SpeedRatio="4" AutoReverse="False"/>
        </Storyboard>
        <Storyboard x:Key="CustomButtonLineBIsCheckedFalse">
            <ColorAnimation From="White" To="{x:Null}" Duration="00:00:01" Storyboard.TargetName="LineB" SpeedRatio="4" AutoReverse="False"/>
        </Storyboard>
        <Storyboard x:Key="CustomButtonLineCIsCheckedTrue">
            <ColorAnimation From="{x:Null}" To="Red" Duration="00:00:01" Storyboard.TargetName="LineC" SpeedRatio="4" AutoReverse="False"/>
        </Storyboard>
        <Storyboard x:Key="CustomButtonLineCIsCheckedFalse">
            <ColorAnimation From="Red" To="{x:Null}" Duration="00:00:01" Storyboard.TargetName="LineC" SpeedRatio="4" AutoReverse="False"/>
        </Storyboard>
    </Window.Resources>
    <Grid>
    <Grid.Children>
                <Border x:Name="AutomaticSystemCalendar_CustomBorder" PreviewMouseLeftButtonDown="AutomaticSystemCalendar_CustomBorder_PreviewMouseLeftButtonDown" Cursor="Hand" CornerRadius="9" BorderBrush="DarkGray" BorderThickness="0.5" Margin="369,371,0,0" Height="24" Width="72" HorizontalAlignment="Left" VerticalAlignment="Top">
                <Border.Background>
                    <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                        <GradientStop Color="#858d68" Offset="0"/>
                        <GradientStop Color="#cbd3ae" Offset="0.5"/>
                        <GradientStop Color="#e9f1cc" Offset="1"/>
                    </LinearGradientBrush>
                </Border.Background>
                <Button x:Name="AutomaticSystemCalendar_CustomButton" Style="{StaticResource CustomButtonStyle}" Margin="0,0,0,-1" HorizontalAlignment="Left" VerticalAlignment="Top" Height="24" Width="24">
                    <Button.Effect>
                        <BlurEffect Radius="0"/>
                    </Button.Effect>
                </Button>
            </Border>
        </Grid.Children>
    </Grid>

Code behind
C#
bool CustomButtonIsChecked = true;
    private void AutomaticSystemCalendar_CustomBorder_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        switch (CustomButtonIsChecked)
        {
            case true:
                (Resources["CustomButtonIsCheckedFalse"] as Storyboard).Begin();
                (Resources["CustomButtonLineAIsCheckedFalse"] as Storyboard).Begin();
                (Resources["CustomButtonLineBIsCheckedFalse"] as Storyboard).Begin();
                (Resources["CustomButtonLineCIsCheckedFalse"] as Storyboard).Begin();
                CustomButtonIsChecked = false;
                break;
            case false:
                (Resources["CustomButtonIsCheckedTrue"] as Storyboard).Begin();
                (Resources["CustomButtonLineAIsCheckedTrue"] as Storyboard).Begin();
                (Resources["CustomButtonLineBIsCheckedTrue"] as Storyboard).Begin();
                (Resources["CustomButtonLineCIsCheckedTrue"] as Storyboard).Begin();
                CustomButtonIsChecked = true;
                break;
        }
    }
Posted
Updated 5-Apr-22 7:33am
v5
Comments
[no name] 30-Apr-21 14:38pm    
You have (object time) architectural issues.

https://stackoverflow.com/questions/8126700/how-do-i-access-an-element-of-a-control-template-from-within-code-behind

https://docs.microsoft.com/en-us/dotnet/desktop/wpf/controls/how-to-create-apply-template?view=netdesktop-5.0

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900