I am writing a custom Hyperlink control(by inheriting from Hyperlink), In my custom style I have multiple textblocks and i want to allow users using my custom control to be able to assign style to these textblocks themselves and apply the static resource style in my resources only when nothing is defined b the user.
MyHyerlink.cs
public partial class MyHyperlink : HyperlinkButton
{
public MyHyperlink()
{
this.DefaultStyleKey = typeof(MyHyperlink);
}
protected override void OnApplyTemplate()
{
_txtTitle = GetTemplateChild(TextTitle) as TextBlock;
_txtContent = GetTemplateChild(TextContent) as TextBlock;
base.OnApplyTemplate();
}
public static readonly DependencyProperty TitleStyleProperty = DependencyProperty.Register(
nameof(TitleStyle),
typeof(Style),
typeof(MyHyperlink),
new PropertyMetadata(null));
public Style TitleStyle
{
get { return (Style)GetValue(TitleStyleProperty); }
set { SetValue(TitleStyleProperty, value); }
}
public static readonly DependencyProperty DescriptionStyleProperty = DependencyProperty.Register(
nameof(DescriptionStyle),
typeof(Style),
typeof(MyHyperlink),
new PropertyMetadata(null));
public Style DescriptionStyle
{
get { return (Style)GetValue(DescriptionStyleProperty); }
set { SetValue(DescriptionStyleProperty, value); }
}
}
MyHyperlink.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Myproject.Controls">
<Style TargetType="TextBlock" x:Key="UrlTitleStyle">
<Setter Property="FontSize" Value="12" />
<Setter Property="FontWeight" Value="Bold" />
</Style>
<Style TargetType="TextBlock" x:Key="UrlDescriptionStyle">
<Setter Property="FontSize" Value="9" />
</Style>
<Style TargetType="local:MyHyperlink">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:MyHyperlink">
<Grid>
<!--Url Part template with parsed image and http content-->
<Border Name="UrlPartTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Image VerticalAlignment="Stretch"
Name="imgLogo"
Grid.Column="0"
/>
<TextBlock Name="txtTitle"
Grid.Column="1"
Margin="5 0"
VerticalAlignment="Center"
TextWrapping="Wrap"
MaxLines="2"
Style="{StaticResource UrlTitleStyle}"
TextTrimming="CharacterEllipsis"/>
<TextBlock Name="txtContent"
Grid.Row="1"
Grid.ColumnSpan="2"
Margin="5, 5"
TextWrapping="Wrap"
MaxLines="3"
Style="{StaticResource UrlDescriptionStyle}"
TextTrimming="CharacterEllipsis" />
</Grid>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
So In the above xaml, controls txtContent and txtTitle should take the style from static resource only when nothing is provided to the TitleStyle and DescriptionStyle dependency props declared in the code.
Can anyone help me out with this, Thanks
You can give default value for property in control's style itself like this.
<Style TargetType="local:MyHyperlink">
<Setter Property="DescriptionStyle" Value="{StaticResource UrlDescriptionStyle}"/>
...
</Style>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With