2011-07-21 7 views
5

次のXAMLでは、ToggleButtonのテンプレートとしてBorderを持つRectangleを使用しています。 ToggleButton.IsCheckedの値の変化を反映するために、BorderBrushを別の色にします。 残念ながら、DataTriggerでTemplateBindingを使用しようとする私の試みは機能しません。代わりに何をする必要がありますか?ControlTemplateのスタイルDataTriggerからのテンプレートバインド

<StackPanel Orientation="Horizontal"> 
    <StackPanel.Resources> 
     <ControlTemplate x:Key="ButtonAsSwatchTemplate"> 
      <Border BorderThickness="1"> 
       <Border.Style> 
        <Style> 
         <Setter Property="BorderBrush" Value="Gainsboro" /> 
         <Style.Triggers> 
          <!-- TemplateBinding doesn't work.--> 
          <DataTrigger        
           Binding={TemplateBinding Property=IsChecked}  
           Value="True"> 
           <Setter Property="BorderBrush" Value="Black" /> 
          </DataTrigger> 
         </Style.Triggers> 
        </Style> 
       </Border.Style> 
       <Rectangle Fill="{TemplateBinding Property=Background}" 
          Width="15" Height="15" /> 
      </Border> 
     </ControlTemplate> 
    </StackPanel.Resources> 
    <ToggleButton Template="{StaticResource ButtonAsSwatchTemplate}" 
        HorizontalAlignment="Center" VerticalAlignment="Center" 
        Margin="2" Background="Red" /> 
</StackPanel> 

私が構築し、私は次のエラーを取得デザイナーリロードすると

EDIT:

エラー1プロパティを 'バインド' タイプ 'TemplateBindingExpression' の値をサポートしていません。あなたは、例えば、ControlTemplateの中にトリガーを使用することができ

SOLUTION

<StackPanel Orientation="Horizontal"> 
    <StackPanel.Resources> 
     <ControlTemplate x:Key="ButtonAsSwatchTemplate">  
      <Border x:Name="SwatchBorder" BorderThickness="1"> 
       <Rectangle Fill="{TemplateBinding Property=Background}" Width="15" Height="15" /> 
      </Border> 
     <ControlTemplate.Triggers> 
      <Trigger Property="ToggleButton.IsChecked" Value="True"> 
       <Setter TargetName="SwatchBorder" Property="BorderBrush" Value="Yellow" /> 
      </Trigger> 
     </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </StackPanel.Resources> 
    <RadioButton Template="{StaticResource ButtonAsSwatchTemplate}" 
     GroupName="CropGuidesColourRadioButtonGroup" 
     IsChecked="{Binding Checked}" Margin="2" Background="Red" /> 
    <RadioButton Template="{StaticResource ButtonAsSwatchTemplate}" 
     GroupName="CropGuidesColourRadioButtonGroup" 
     IsChecked="{Binding Checked}" Margin="2" Background="Black" /> 
    ... 
</StackPanel> 

答えて

5

<StackPanel Orientation="Horizontal"> 
    <StackPanel.Resources> 
     <ControlTemplate x:Key="ButtonAsSwatchTemplate"> 
      <Border x:Name="myBorder" BorderBrush="Gainsboro" BorderThickness="1"> 
       <Rectangle Fill="{TemplateBinding Property=Background}" Width="15" Height="15" /> 
      </Border> 
      <ControlTemplate.Triggers> 
       <Trigger Property="ToggleButton.IsChecked" Value="True"> 
        <Setter TargetName="myBorder" Property="BorderBrush" Value="Black" /> 
       </Trigger> 
      </ControlTemplate.Triggers> 
     </ControlTemplate> 
    </StackPanel.Resources> 
    <ToggleButton Template="{StaticResource ButtonAsSwatchTemplate}" 
       HorizontalAlignment="Center" VerticalAlignment="Center" 
       Margin="2" Background="Red" /> 
</StackPanel> 
+0

これをありがとう。私はControlTemplateのトリガーでTemplateBindingsを実行しようとしていましたが、コントロールテンプレートのコントロールのDependencyPropertiesにアクセスすることができませんでした –

0

すぐにわかる問題の1つは、BorderBrushをBorderのローカルプロパティ値として設定していることです。これは常にスタイルによって適用される値を上書きします。明示的なBorderBrush属性を削除し、それが機能するかどうか確認してください。

ボーダー BorderBrush = "Gainsboro" BorderThickness = "1"

Dependency Property Value Inheritance

+0

良いコール、私はそれを私のコードから削除し、私の質問でサンプルを更新しました私はまだ編集として上記で追加したエラーを取得します。 – Grokodile

関連する問題