Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

defined my own custom button behaviour, how can i re-use the same xaml and just specify different images to be used?

$
0
0

Hello All,

I have defined my own custom button - it is essentially an image, that will react when you hover over it, or when you click it. Here is the button definition:

<Window.Resources><Style x:Key="MyCustomButton" TargetType="{x:Type Button}"><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type Button}"><Grid><Image ToolTip="Hello World!" Source="/CustomShapedApplication;component/Images/sheepish-icon.png"/></Grid><ControlTemplate.Triggers><Trigger Property="IsFocused" Value="True"/><Trigger Property="IsDefaulted" Value="True"/><Trigger Property="IsMouseOver" Value="True"><Setter Property="RenderTransform" ><Setter.Value><ScaleTransform ScaleX="1.1" ScaleY="1.1" /></Setter.Value></Setter></Trigger><Trigger Property="IsPressed" Value="True"><Setter Property="RenderTransform" ><Setter.Value><ScaleTransform ScaleX=".95" ScaleY=".95" /></Setter.Value></Setter></Trigger><Trigger Property="IsEnabled" Value="False"/></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style></Window.Resources>

the button definition at the point where I define it is then:

<Button x:Name="imageBtn1" Style="{StaticResource MyCustomButton}" Height="50" Width="55" Margin="40,112,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" />

As you can see, I specify the image to be used by the button in the Style definition, rather as per button. 

You already see where I am going with this dontcha? Well, now there is a need for more than one of this particular button type, and I do not want to copy and paste the entire definition, just to change a small property like the image source. Is there a way that I can modify the above, in order to take the image source as a parameter, or have it specified as part of the Button definition? This way, I will be able to have different images for my buttons, and the underlying behaviour is still the same. 

Hope you understand my question, if you need more info, please feel free to ask.

Thanks

Harriet


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>