Reputation: 1289
I'm beginning to work on XAML controls in a UWP App.
I want to create several variations of a same control (some kind of UI library), that will be reused in the app, let's say a "Big button" and a "Small button".
Is there any performance problems, drawbacks, or better way to achieve the "custom tagname" control personalization ?
(I personally found it way more readable and less error-prone once each control has been stylized.)
Thanks a lot!
The "common" way seems to create styles / templates based on Button, like so :
Styles.xaml (ResourceDictionary)
<Style x:Key="MyBigButtonStyle" TargetType="Button">
<Setter Property="FontSize" Value="36"/>
</Style>
<Style x:Key="MySmallButtonStyle" TargetType="Button">
<Setter Property="FontSize" Value="10"/>
</Style>
And then declare the controls as follow :
MyView.xaml (Page)
<Button Style="{StaticResource MyBigButtonStyle}">My Big Button</Button>
<Button Style="{StaticResource MySmallButtonStyle}">My Small Button</Button>
The "custom tagname" way, for the sake of readability and simplicity (with custom as the correct namespace usage) :
MyCustomButtons.cs
namespace MyApp.MyCustomComponents
{
public sealed class MyBigButton : Button {}
public sealed class MySmallButton : Button {}
}
Styles.xaml
<Style Target="custom:MyBigButton">
<Setter Property="FontSize" Value="36"/>
</Style>
<Style Target="custom:MySmallButton">
<Setter Property="FontSize" Value="10"/>
</Style>
MyView.xaml
<custom:MyBigButton>My Big Button</custom:MyBigButton>
<custom:MySmallButton>My Small Button</custom:MySmallButton>
Upvotes: 0
Views: 128
Reputation: 169160
Whether to create a custom control or re-template or re-styling an existing one depends entirely on your requirements. There are no "performance problems" involved in the decision.
If you don't intend to add any new functionality to a control, it doesn't make much sense to create another class like this:
public sealed class MyBigButton : Button {}
Then you might as well simply create a custom style or template.
Please refer to Jerry Nixon's MSDN Magazine article about how to create custom controls in XAML for more information.
Upvotes: 1