Home » c# » c# – Trigger not working on custom control image button wpf-Exceptionshub

c# – Trigger not working on custom control image button wpf-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I have created one custom cotrol

Generic.xaml code is below –

<BitmapImage x:Key="RightImaGE" x:Name="imgD" UriSource="/XYZ.UI_Test;components/Resources/Pfad55-1.png"/>

    <Style TargetType="{x:Type local1:CustomButton}" >
        <Setter Property="Template">
             <Setter.Value>
                <ControlTemplate TargetType="{x:Type local1:CustomButton}">
                    <!--<Image Name="imgDefault" Source="{TemplateBinding Image}"  Stretch="UniformToFill" />-->
                    <ControlTemplate.Triggers>

                        <DataTrigger Binding="{Binding Path=ContentOff, RelativeSource={RelativeSource  AncestorType=local1:CustomButton}}" Value="SETTINGS">
                            <Setter Property="Background" Value="Red"/>
                            <Setter  Property="Image.Source" Value="{DynamicResource RightImaGE}" />
                        </DataTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

CustomButton.cs codebehind is below

public string ContentOff
        {
            get { return (string)GetValue(ContentOffProperty); }
            set { SetValue(ContentOffProperty, value); }
        }
        public static readonly DependencyProperty ContentOffProperty =
            DependencyProperty.Register("ContentOff",
            typeof(string), typeof(CustomButton), new PropertyMetadata(null));

        public ImageSource Image
        {
            get { return (ImageSource)GetValue(ImageProperty); }
            set { SetValue(ImageProperty, value); }
        }

        public static readonly DependencyProperty ImageProperty =
            DependencyProperty.Register("Image", typeof(ImageSource), typeof(CustomButton), new PropertyMetadata(default(ImageSource)));

I have defined one common style in app.xaml code is below

<Application.Resources>
        <Style x:Key="BigButtonStyle" TargetType="local:CustomButton" >
            <Setter Property="Control.Background" Value="Transparent"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="HorizontalAlignment" Value="Left"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="Margin" Value="1"/>
            <Setter Property="FontSize" Value="27"/>
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="Foreground" Value="#1d5560"/>
            <Setter Property="Width" Value="170"/>
            <Setter Property="MaxHeight" Value="50"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:CustomButton}">
                        <Canvas Margin="1">
                            <Image x:Name="img" Source="Resources/Pfad55.png"  Stretch="UniformToFill" >
                                <Image.Style>
                                    <Style TargetType="{x:Type Image}">
                                        <Setter Property="Source" Value="Resources/Pfad55.png" />
                                        <!--<Style.Triggers>
                                            --><!--<Trigger Property="ContentOff" Value="SETTINGS">
                                                <Setter Property="Opacity" Value="0.5" />
                                            </Trigger>--><!--
                                        </Style.Triggers>-->
                                    </Style>
                                </Image.Style>
                            </Image>
                            <ContentPresenter HorizontalAlignment="Center" Content="{TemplateBinding ContentOff}" Canvas.Top="14" Canvas.Left="47"
                                      VerticalAlignment="Center"/>
                        </Canvas>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

Now I have Mainwindows.xaml where I am using Custombutton and if button content is Settings it should change different image through trigger than common image which is defined in bigbuttonstyle .

<Controls:CustomButton  Grid.Row="8" Grid.Column="2" Style="{StaticResource BigButtonStyle}"
                               ContentOff="SETTINGS" 
                               Image="Resources/Pfad55-1.png"/>

I tried to use data trigger here but not working and tried property trigger too. But image not changing somehow.

How to&Answers:

You can use two different style with style inheritance –

 <Style x:Key="SmallButtonStyle" TargetType="local:CustomButton" BasedOn="{StaticResource BigButtonStyle}">
           <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:CustomButton}">
                        <Canvas Margin="1">
                            <Image Source="Resources/Pfad55-1.png"  Stretch="UniformToFill"  ></Image>
                            <ContentPresenter HorizontalAlignment="Center" Content="{TemplateBinding ContentOff}" Canvas.Top="14" Canvas.Left="85"
                                      VerticalAlignment="Center"/>
                        </Canvas>

                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

then use it

    <Controls:CustomButton  Grid.Row="8" Grid.Column="2" Style="{StaticResource SmallButtonStyle}"
                       ContentOff="SETTINGS" 
                       Image="Resources/Pfad55-1.png"/>

It’ll be working but without trigger.

Another solution I have added based on your requirement.
Change your Generic.Xaml code like below.

<Style TargetType="{x:Type local1:CustomButton}" >
    <Setter Property="FontSize" Value="27"/>
    <Setter Property="FontWeight" Value="Bold"/>
    <Setter Property="Foreground" Value="#1d5560"/>
    <Setter Property="Template">
         <Setter.Value>                
            <ControlTemplate TargetType="{x:Type local1:CustomButton}">
                <Grid>
                    <Image  Stretch="Fill" >
                        <Image.Style>
                            <Style TargetType="{x:Type Image}">
                                <Setter Property="Source" Value="Resources/Pfad55.png" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=local1:CustomButton},Path=ContentOff}"
                                        Value="SETTINGS">
                                        <Setter Property="Source"
                                                Value="Resources/Pfad55-1.png" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Image.Style>
                    </Image>
                    <ContentPresenter HorizontalAlignment="Left" Margin="50,0,0,0" Content="{TemplateBinding ContentOff}" 
                                  VerticalAlignment="Center"/>
                </Grid>
            </ControlTemplate>

        </Setter.Value>
    </Setter>
</Style>

And Chnage your MainWindow.xaml implementation with this

<Controls:CustomButton  Grid.Row="8" Grid.Column="2" ContentOff="SETTINGS"/>

You were using App.xaml style with BigButtonStyle , that’s why custom control styles and trigger was not firing. Your App.xaml style was locally overriding custom control style and trigger from Generic.xaml.