2017-03-16 51 views
3

我正在編寫自定義超鏈接控件(通過繼承超鏈接),在我的自定義樣式中,我有多個文本塊,並且我想允許用戶使用我的自定義控件只有當用戶沒有定義任何內容時,才能爲這些文本塊自身分配樣式,並在我的資源中應用靜態資源樣式。如何在靜態資源中爲UWP中的依賴屬性添加默認值

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,控件txtContent和txtTitle應該從靜態資源的風格,提供什麼,只有當到代碼中聲明的TitleStyle和DescriptionStyle依賴項支持。

誰能幫我這個,感謝

+0

你試試這個'公共靜態只讀的DependencyProperty TitleStyleProperty = DependencyProperty.Register( nameof(TitleStyle), typeof運算(風格), typeof運算(OpacityMask), 新PropertyMetadata(Application.Current.Resources [」 UrlDescriptionStyle「]));' – Archana

+0

是的,我做了...問題是我創建的控件是在一個單獨的項目(類庫)完全與應用程序..所以我定義的靜態資源將不存在應用程序資源中,我需要在類庫本身中定義我的默認樣式,我已經嘗試在相同的資源字典中定義那些控制模板派生自的源文件,但它似乎沒有找到。 – KNara

+0

嘗試給控制風格內的價值? Like' ' – Archana

回答

2

可以在控件的樣式本身這樣給默認值屬性。

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