c# - 如何从静态资源为 UWP 中的依赖属性添加默认值

标签 c# windows xaml uwp uwp-xaml

我正在编写一个自定义超链接控件(通过从超链接继承),在我的自定义样式中,我有多个文本 block ,我希望允许使用我的自定义控件的用户能够自己为这些文本 block 分配样式并应用静态资源仅当用户未定义任何内容时才在我的资源中设置样式。

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); }
    }
}

我的超链接.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>

所以在上面的 xaml 中,只有当代码中声明的 TitleStyle 和 DescriptionStyle 依赖项没有提供任何内容时,控件 txtContent 和 txtTitle 才应该从静态资源中获取样式。

谁能帮我解决这个问题,谢谢

最佳答案

您可以像这样在控件样式本身中为属性提供默认值。

<Style TargetType="local:MyHyperlink">
<Setter Property="DescriptionStyle" Value="{StaticResource UrlDescriptionStyle}"/>
...
</Style>

关于c# - 如何从静态资源为 UWP 中的依赖属性添加默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42832097/

相关文章:

php - 如何降级或安装特定版本的 Composer?

xaml - Xamarin Forms ListView 数据绑定(bind)

C# 如何实现具体类不同的接口(interface)?

c# - 取消从应用程序调用网络服务

c++ - "APIENTRY _tWinMain"和"WINAPI WinMain"的区别

c# - 如何在 C#/XAML Windows 应用商店 (Metro UI) 应用程序中混合浅色和深色主题?

c# - 从数据网格中获取对象类型

c# - 使用 ASP.NET 重置密码时 token 无效

c# - 我将如何在 EF Core 中创建这种多对多关系?

windows - "The Ordinal 112 could not be located in dynamic link library..."