【问题标题】:How to add default value for a dependency property in UWP from static resource如何从静态资源中为 UWP 中的依赖属性添加默认值
【发布时间】:2017-03-16 10:57:27
【问题描述】:

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

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>

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

谁能帮帮我,谢谢

【问题讨论】:

  • 你试过这个public static readonly DependencyProperty TitleStyleProperty = DependencyProperty.Register( nameof(TitleStyle), typeof(Style), typeof(OpacityMask), new PropertyMetadata(Application.Current.Resources["UrlDescriptionStyle"]));
  • 是的,我做到了...问题是我创建的控件与应用程序完全位于一个单独的项目(类库)中。所以我定义的静态资源不会存在于应用程序资源中,我需要在类库本身中定义我的默认样式,我试图在同一个资源字典中定义那些控件模板派生的资源字典,但它似乎没有找到。
  • 尝试在控件的样式中赋予价值?点赞&lt;Setter Property="DescriptionStyle" Value="{StaticResource UrlDescriptionStyle}"/&gt;
  • 不确定这将如何工作...我需要将此特定样式应用于自定义控件中定义的控件之一...而且它会变成硬编码对吗?它无法应用从 Application xaml 分配给依赖属性的任何样式
  • 您可以稍后在 xaml 中更改其值。它将如何变成硬编码?它是一个默认值。如果您不分配任何值,则将采用此值。

标签: c# windows xaml uwp uwp-xaml


【解决方案1】:

您可以像这样为控件样式本身的属性赋予默认值。

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

【讨论】:

    猜你喜欢
    • 2011-08-19
    • 2011-08-19
    • 2013-10-09
    • 2015-10-17
    • 1970-01-01
    • 2011-12-01
    • 2011-02-05
    • 1970-01-01
    相关资源
    最近更新 更多