C# 如何从静态资源为UWP中的依赖项属性添加默认值
我正在编写一个自定义超链接控件(通过继承超链接),在我的自定义样式中,我有多个文本块,我希望允许使用我的自定义控件的用户能够将样式分配给这些文本块,并且仅当用户未定义任何内容时,才在我的资源中应用静态资源样式 MyHyerlink.csC# 如何从静态资源为UWP中的依赖项属性添加默认值,c#,windows,xaml,uwp,uwp-xaml,C#,Windows,Xaml,Uwp,Uwp Xaml,我正在编写一个自定义超链接控件(通过继承超链接),在我的自定义样式中,我有多个文本块,我希望允许使用我的自定义控件的用户能够将样式分配给这些文本块,并且仅当用户未定义任何内容时,才在我的资源中应用静态资源样式 MyHyerlink.cs public partial class MyHyperlink : HyperlinkButton { public MyHyperlink() { this.DefaultStyleKey = typeof(MyHyperli
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中,控件txtContent和txtTitle只有在代码中声明的TitleStyle和DescriptionStyle依赖项道具没有提供任何内容时才应该采用静态资源中的样式
有人能帮我解决这个问题吗,谢谢你可以像这样给控件样式本身中的属性指定默认值
<Style TargetType="local:MyHyperlink">
<Setter Property="DescriptionStyle" Value="{StaticResource UrlDescriptionStyle}"/>
...
</Style>
...
您是否尝试过此公共静态只读DependencyProperty TitleStyleProperty=DependencyProperty.Register(名称(标题样式)、类型(样式)、类型(不透明掩码)、新属性元数据(Application.Current.Resources[“UrlDescriptionStyle”])代码>是的,我做了…问题是我创建的控件与应用程序一起位于单独的项目(类库)中..因此我定义的静态资源将不存在于应用程序资源中,我需要在类库本身中定义我的默认样式,我已尝试在控件模板派生的同一资源字典中为其定义这些样式,但它似乎找不到。是否尝试在控件的样式中指定值?就像
不确定这将如何工作…我需要将此特定样式应用于自定义控件中定义的一个控件…而且它将成为硬编码的,对吗?它将无法应用从应用程序xaml分配给依赖项属性的任何样式。您可以稍后在xaml中更改其值。它将如何变成硬编码?这是一个默认值。如果不指定任何值,将采用该值。