2014-10-17 1 views
0

내 단추의 ControlTemplate VisualStateManager에서 바인딩 할 속성이 있습니다.VisualState를 사용하는 ControlTemplate 연결된 속성에 대한 ColorAnimation 바인딩

아래 코드는 속성에 대한 코드와 버튼 스타일에 대한 XAML입니다. 오류나 경고가 없지만 단추 위에 마우스를 올리거나 단추를 눌렀을 때 단추의 색이 바뀌지 않습니다.

무엇이 문제입니까? 나는 다음과 같은 예제를 검토 한 결과 그들이 지금까지 근무 한 적이없는 :

How to use Attached property within a style?

다음

Problem reading AttachedProperty in ControlTemplate

은이 호텔의 :

xmlns:attached="clr-namespace:company.project.Utilities.AttachedBehaviors;assembly=company.project.Utilities" 

<Style x:Key="DialogButtonStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource StandardButtonStyle}"> 
    <Setter Property="Background" Value="{StaticResource ElectroTekGrayBrush}" /> 
    <Setter Property="attached:Attached.MouseOverColor" Value="{StaticResource ElectroTekGreen}" /> 
    <Setter Property="attached:Attached.PressedColor" Value="{StaticResource ElectroTekLightGray}" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Button}"> 
       <Grid> 
        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="CommonStates"> 
          <VisualState x:Name="Normal" /> 
          <VisualState x:Name="MouseOver"> 
           <Storyboard> 
            <ColorAnimation Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" Storyboard.TargetName="Border" To="{Binding Path=(attached:Attached.MouseOverColor), RelativeSource={RelativeSource AncestorType=Button}}" Duration="0" /> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Pressed"> 
           <Storyboard> 
            <ColorAnimation Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" Storyboard.TargetName="Border" To="{Binding Path=(attached:Attached.PressedColor), RelativeSource={RelativeSource AncestorType=Button}}" Duration="0" /> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Disabled" /> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
        <Border Name="Border" Background="{TemplateBinding Background}" CornerRadius="{Binding RelativeSource={RelativeSource Self}, Path=ActualHeight, Converter={StaticResource MathConverter}, ConverterParameter=(@VALUE/2)}" IsHitTestVisible="True"> 
         <Grid> 
          <TextBlock x:Name="TextBlock" Text="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" FontSize="15" /> 
         </Grid> 
        </Border> 
       </Grid> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsMouseOver" Value="True"> 
         <Setter TargetName="TextBlock" Property="Cursor" Value="Hand" /> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
: 여기

using System.Windows; 
using System.Windows.Media; 

namespace company.project.Utilities.AttachedBehaviors 
{ 
    public class Attached : DependencyObject 
    { 
     // Attached properties... 
     #region MouseOverColor Property 

     public static readonly DependencyProperty MouseOverColorProperty = DependencyProperty.RegisterAttached(
       "MouseOverColor", 
       typeof(Color), 
       typeof(Attached) 
      ); 

     public static void SetMouseOverColor(UIElement element, Color value) 
     { 
      element.SetValue(MouseOverColorProperty, value); 
     } 

     public static Color GetMouseOverColor(UIElement element) 
     { 
      return (Color)element.GetValue(MouseOverColorProperty); 
     } 

     #endregion 

     #region PressedColor Property 

     public static readonly DependencyProperty PressedColorProperty = DependencyProperty.RegisterAttached(
       "PressedColor", 
       typeof(Color), 
       typeof(Attached) 
      ); 

     public static void SetPressedColor(UIElement element, Color value) 
     { 
      element.SetValue(PressedColorProperty, value); 
     } 

     public static Color GetPressedColor(UIElement element) 
     { 
      return (Color)element.GetValue(PressedColorProperty); 
     } 

     #endregion 
    } 
} 

스타일이다

버튼의 위치 :

<Button Style="{StaticResource DialogButton}" /> 
+1

'스토리 보드'에서 '수신'또는 '보낸 사람'에 대해 '바인딩'을 사용할 수 없습니다. 이 경우에 오류가없는 것도 이상한 일입니다. –

+0

좋아요, 트리거로 전환하려고합니다. –

답변

0

좋아, 나는 @ 킹 킹이받는 사람과받는 사람 속성에 대해 정확하고 방금 트리거를 사용하여 동일한 기능을 구현했다고 가정했습니다. 아무도 그것을 필요로한다면 스타일이 있습니다.

<Style x:Key="DialogButtonStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource StandardButtonStyle}"> 
    <Setter Property="Background" Value="{StaticResource ElectroTekGrayBrush}" /> 
    <Setter Property="attached:Attached.MouseOverColor" Value="{StaticResource ElectroTekGreen}" /> 
    <Setter Property="attached:Attached.PressedColor" Value="{StaticResource ElectroTekOrange}" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Button}"> 
       <Grid> 
        <Border Name="border" Background="{TemplateBinding Background}" CornerRadius="{Binding RelativeSource={RelativeSource Self}, Path=ActualHeight, Converter={StaticResource MathConverter}, ConverterParameter=(@VALUE/2)}" IsHitTestVisible="True" Cursor="Hand"> 
         <TextBlock x:Name="TextBlock" Text="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" FontSize="15" Cursor="Hand" /> 
        </Border> 
       </Grid> 
       <ControlTemplate.Resources> 
        <SolidColorBrush x:Key="MouseOverBrush" Color="{Binding Path=(attached:Attached.MouseOverColor), RelativeSource={RelativeSource AncestorType=Button}}" /> 
        <SolidColorBrush x:Key="PressedBrush" Color="{Binding Path=(attached:Attached.PressedColor), RelativeSource={RelativeSource AncestorType=Button}}" /> 
       </ControlTemplate.Resources> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsMouseOver" Value="True" SourceName="border"> 
         <Setter Property="Background" Value="{StaticResource MouseOverBrush}" TargetName="border" /> 
        </Trigger> 
        <Trigger Property="IsPressed" Value="True"> 
         <Setter Property="Background" Value="{StaticResource PressedBrush}" TargetName="border" /> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
+1

실제 애니메이션을 위해 스토리 보드를 사용하지 않는 한 좋습니다. 이자형. Duration을 0으로 설정하십시오. 저는 애니메이션과 함께 이것을 필요로하므로 제 경우에는 트리거를 사용할 수 없습니다. 다른 곳에서는 애니메이션을 동결해야하고 바인딩을 사용할 수 없다고 읽었습니다. – ygoe

관련 문제