2017-02-02 1 views
2

Application.ResourcesResourceDictionaryのエントリがスリム化、コントロールテンプレートでは、次のようになります。RelativeSourceバインディングスタイルの作品ではないのControlTemplate

<ControlTemplate TargetType="{x:Type ToggleButton}"> 
    <Border> 
     <Border.BorderBrush> 
      <SolidColorBrush Color="{Binding Path=BorderColor, RelativeSource={RelativeSource AncestorType=UserControl}" /> 
     </Border.BorderBrush> 
    </Border> 
</ControlTemplate> 

UserControlは独自の性質を持っている、これはからプルBorderColor 。この例では、バインディングはプロパティを見つけることができません。

は ' 'RelativeSource FindAncestor、AncestorType =' System.Windows.Controls.UserControl'、 AncestorLevel = '1' の参照との結合のためのソースを見つけることができません。

しかし、それは辞書内の別のエントリで動作します:

<Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}"> 
    <Setter Property="BorderBrush" Value="{Binding Path=BorderColor, RelativeSource={RelativeSource AncestorType=UserControl}"/> 
</Style> 

私は最初の例では結合を解決することができますか?私は、ユーザーコントロールの各コントロールのインスタンスに追加のプロパティを必要としないことをお勧めします。

答えて

1

つの提案:

のControlTemplateを使用すると、結合とSolidColorBrushにトグルボタンのBorderBrushプロパティを設定し、テンプレートでTemplateBindingを使用することができますスタイルの一部である場合:

<Style x:Key="myStyle" TargetType="ToggleButton"> 
    <Setter Property="BorderBrush"> 
     <Setter.Value> 
      <SolidColorBrush Color="{Binding Path=BorderColor, RelativeSource={RelativeSource AncestorType=UserControl}}" /> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ToggleButton}"> 
       <Border BorderBrush="{TemplateBinding Background}" BorderThickness="10"> 
        <TextBlock>....</TextBlock> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

場合Colorプロパティの代わりにBrushプロパティにバインドするのが何らかの理由でスタンドアロンのControlTemplateを定義したいと考えています。

+0

私は最初の例のように「スタイル」にするためにテンプレートをいくつか再編集しました。 'TemplateBinding'を使って作業しました、ありがとう!今それを動的に更新するには... – Dan

0

BorderColorの代わりにBackgroundを使用すると機能します。 BorderColorは独自のプロパティですか?

<Window.Resources> 
<ControlTemplate x:Key="template" TargetType="{x:Type ToggleButton}"> 
    <Border> 
     <Border.BorderBrush> 
       <SolidColorBrush Color="{Binding Path=Background,RelativeSource={RelativeSource AncestorType=UserControl}}" /> 
     </Border.BorderBrush> 
    </Border> 
</ControlTemplate> 
</Window.Resources> 

<UserControl Background="Aqua"> 
    <ToggleButton Template="{StaticResource template}"></ToggleButton> 

</UserControl> 
関連する問題