gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:27:53 25 4
gpt4 key购买 nike

我正在编写一个自定义超链接控件(通过从超链接继承),在我的自定义样式中,我有多个文本 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com