Reputation: 2472
I created a custom button and would like to put a style on the button. I would like to put the style on the button at the class level so that I don't have to set the Style property on every button instance. Is there a way to do this? I have a Application Resource file that contains the styles for the button, just need to link the two up.
Button Class
public class CfcButton : Button
{
public CfcButton()
{
//Set the Style here
}
}
then when I have to put a button on the xaml page it will just be like so:
<controls:CfcButton Name="btnSearch" Margin="15,0,0,0"
Content="Search" Width="75"
Command="{Binding SearchClick}" />
instead of setting the style on the button when it is created like so:
<controls:CfcButton Name="btnSearch" Margin="15,0,0,0"
Content="Search" Width="75"
Style="{DynamicResource CfcButtonStyle}"
Command="{Binding SearchClick}" />
Upvotes: 0
Views: 295
Reputation: 10460
You can set up the style in the constructor like this:
this.Style = (Style)Application.Current.Resources["CfcButtonStyle"];
and if your style resource doesn't have a key, but only a target type, then you can use typeof(CfcButton) as the resource key - but now I can see, that it has, so I updated the code...
Upvotes: 0
Reputation: 81313
Place the default style (without any x:Key set on it) under App resources and it will be automatically applied to all custom buttons in your application.
<Application>
<Application.Resources>
<Style xmlns:local="clr-namespace:YourNamespace"
TargetType="local:CfcButton">
......
</Style>
</Application.Resources>
</Application>
Replace YourNamespace with actual namespace name where CfcButton class is declared in.
Upvotes: 1