2017-06-23 1 views
0

は、私はstackoverflowの中に以下のコードを見て:XAMLでusercontrolの子のスタイルを使用しますか?

<UserControl x:Class="WpfApplication3.UserControl1" 
       x:Name="Uc1" 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
     <UserControl.Resources> 
      <Style TargetType="Label"> 
       <Setter Property="Foreground" 
         Value="{Binding Foreground, ElementName=Uc1, Mode=OneWay}"/> 
      </Style> 
     </UserControl.Resources> 

     <Grid>    
      <Label Content="Label 1"/> 
      <Label Content="Label 2"/> 
     </Grid> 
    </UserControl> 

質問:私はスタイリングのための私のusercontrol.resources内の特定のラベルをターゲットにすることができた場合、私は今、不思議。私のuserControlの中で可能ですか?もしそうなら、どのように?

答えて

1

Keyのないスタイルは、スコープ内のターゲットタイプのすべてのインスタンスに適用されます。

スタイルKey、等を付け、

<Style TargetType="Label" x:Key="MyLabel"> 

し、次のようにキーを使用

<Label Content="Label 1" Style="{StaticResource MyLabel}" /> 

<!--Will not apply the style to Label 2--> 
<Label Content="Label 2"/> 

編集:私は再びあなたの質問を読んで

、あなたがしたいようですStyleからTargetを参照するために、参照番号StyleTarget。そうですか? が不自然でと聞こえますが、それは派生クラスのインスタンスの名前を基底クラスから知りたいのと同じです。

+0

回答を受け入れました:) – Asperger

関連する問題