Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add default value for a dependency property in UWP from static resource

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

like image 892
KNara Avatar asked Dec 27 '25 20:12

KNara


1 Answers

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>
like image 180
Archana Avatar answered Dec 30 '25 14:12

Archana



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!