2017-07-27 4 views
2

項目に追加することができるプロパティの組み合わせを定義する方法はありますか?要素プロパティの継承と再利用XAMLベタープラクティス

ので、代わりに:

<TextBlock Text="Hi" Foreground="Gray" Margin="0,8,0,0" FontFamily="Segoe UI"/> 
<TextBlock Text="there" Foreground="Gray" Margin="0,8,0,0" FontFamily="Segoe UI"/> 

私は本当にメンテナンス性と可読性そんなに簡単に....

ちょうどような何かを作る

<PropertyCombo name="textBlockCombo1" 
    Foreground="Gray" Margin="0,8,0,0" FontFamily="Segoe UI" 
. 
. 
. 
<TextBlock Text="Hi" ImportProperty="textBlockCombo1"/> 
<TextBlock Text="there" ImportProperty="textBlockCombo1"/> 

答えて

1

雅、のような何かをするだろうこれは、親インスタンスレベルで、またはDOMからのように継承のためにツリーをさらに上にしています。

<StackPanel> 
    <StackPanel.Resources> 
     <Style TargetType="TextBlock"> 
     <Setter Property="FontSize" Value="30"/> 
     <Setter Property="FontWeight" Value="Bold"/> 
     <Setter Property="Foreground" Value="Green"/> 
     <Setter Property="Margin" Value="5,10"/> 
     <Setter Property="Text" Value="Blah"/> 
     </Style> 
    </StackPanel.Resources/> 

    <TextBlock/> 
    <TextBlock/> 
    <TextBlock/> 
    <TextBlock/> 
    <TextBlock/> 

</StackPanel> 

また、単にX作ることができます。キーの静的なスタイルをし、リソースディクショナリでそれらを投げると、インスタンスでのように、必要な場所にそれらを呼び出します。

<Style TargetType="TextBlock" x:Key="AwesomeStyleName"> 
    <Setter Property="FontSize" Value="30"/> 
    <Setter Property="FontWeight" Value="Bold"/> 
    <Setter Property="Foreground" Value="Green"/> 
    <Setter Property="Margin" Value="5,10"/> 
    <Setter Property="Text" Value="Blah"/> 
</Style> 

次にインスタンスを起動します。

<TextBlock Style="{StaticResource AwesomeStyleName}"/> 

たとえば、次のようなインスタンスのグループに適用します。

<StackPanel> 
    <StackPanel.Resources> 
     <Style TargetType="TextBlock" BasedOn="{StaticResource AwesomeStyleName}"/> 
    </StackPanel.Resources/> 

    <TextBlock/> 
    <TextBlock/> 
    <TextBlock/> 

</StackPanel> 

これがうまくいくことを願っています。私がまさに必要

+0

、おかげでたくさん! – isebarn

+0

いつでも、素晴らしい週末を持っています! –

関連する問題