1

WPF GroupBox 컨트롤을 사용자 정의하고 있습니다. 마우스 포인터가 컨트롤 영역으로 들어갈 때 배경에 대한 컬러 애니메이션을 구현해야합니다. 예를 들어, 배경색을 미리 정의 된 색상으로 천천히 변경하십시오 (핑크색으로 변경). 이를 위해 사용자 지정 컨트롤 템플릿을 만들었습니다. 중요한 부분은 다음과 같습니다.ControlTemplate 내의 WPF Grid.Background에 대한 ColorAnimation에 대한 InvalidOperationException

<ControlTemplate TargetType="{x:Type GroupBox}"> 
    <Grid Name="MainGrid" SnapsToDevicePixels="true"> 
     <!-- Control layout stuff with ContentPresenter --> 
    </Grid> 

    <ControlTemplate.Triggers> 
     <EventTrigger RoutedEvent="MouseEnter"> 
      <BeginStoryboard> 
       <Storyboard> 
        <ColorAnimation 
         Storyboard.TargetName="MainGrid" 
         Storyboard.TargetProperty="Background.(SolidColorBrush.Color)" 
         To="Pink" Duration="0:0:1" /> 
       </Storyboard> 
      </BeginStoryboard> 
     </EventTrigger> 
    </ControlTemplate.Triggers> 
</ControlTemplate> 

그러나이 애니메이션은 작동하지 않습니다.

'System.InvalidOperationException'는이 같은 추가 정보 PresentationFramework.dll에서 발생 : 난 항상 형의 처리되지 않은 예외 얻을. '배경'속성 경로 '배경에 DependencyObject에 가리하지 않습니다 (0) '

나는이 문제를 봤다. 그것은, TargetProperty에 대한 올바른 구문을 사용하여 애니메이션을 적용해야합니다. 그러나, 나는 다음과 같은 것과 같은 변형을 많이 시도하고 그들은 모두 내 경우에는 작동하지 않습니다 내가

  • Background.Color
  • (Panel.Background).Color
  • (Panel.Background).(SolidColorBrush.Color)
  • (Grid.Background).(SolidColorBrush.Color)

를 검색 오전 올바른 방향이 아닌가?

+0

그냥 사용할 수 있습니다'TargetProperty = Background' 작동합니다. 브러시로'Pink'를 인식하지 못한다면,'StaticResource'를 사용해야 할 수도 있지만, 미리 정의 된 색상 이름 인 경우 이름만으로 작동해야합니다. – Adwaenyth

+0

@ Adwaenyth, 작동하지 않는지 확인하십시오. 추가 정보 : 'System.Windows.Media.Animation.ColorAnimation'애니메이션 개체는 'System.Windows.Media.Brush'형식이 ​​호환되지 않으므로 'Background'속성에 애니메이션을 적용 할 수 없습니다. – TecMan

+0

Mh ...'Storyboard.TargetProperty = (GroupBox.Background). (SolidColorBrush.Color)'로 시도해보십시오. 그러면 다음과 같이 작동합니다. [here] (http://stackoverflow.com/questions/14158500/wpf-animate) -배경색)? – Adwaenyth

답변

1

TargetElement은 애니메이션을 작동 시키려면 스타일에 초기 Setter가 필요합니다.

<GroupBox> 
     <GroupBox.Style> 
      <Style TargetType="GroupBox"> 
       <Style.Setters> 
        <Setter Property="Template"> 
         <Setter.Value> 
          <ControlTemplate TargetType="{x:Type GroupBox}"> 
           <Grid Name="MainGrid" SnapsToDevicePixels="true"> 
            <Grid.Style> 
             <Style TargetType="Grid"> 
              <Setter Property="Background" Value="Blue"></Setter> 
             </Style> 
            </Grid.Style> 
            <ContentPresenter/> 
           </Grid> 
           <ControlTemplate.Triggers> 
            <EventTrigger RoutedEvent="MouseEnter"> 
             <BeginStoryboard> 
              <Storyboard> 
               <ColorAnimation Storyboard.TargetName="MainGrid" Storyboard.TargetProperty="Background.Color" To="Pink" Duration="0:0:1" /> 
              </Storyboard> 
             </BeginStoryboard> 
            </EventTrigger> 
           </ControlTemplate.Triggers> 
          </ControlTemplate> 
         </Setter.Value> 
        </Setter> 
       </Style.Setters> 
      </Style> 
     </GroupBox.Style> 
     <Button Content="test" Width="200" Height="50"></Button> 
    </GroupBox> 

이 작업을 수행 한 후, 당신은 쉽게 Storyboard.TargetProperty="Background.Color"

+0

네, 정말로, 저는 이것에 대해서도 생각했습니다 : ColorAnimation은 어떻게 설정되지 않은 색상에서 핑크색으로 애니메이션을 적용 할 수 있는지 알고 있습니까? 그러나이를 보편적 인 방식, 즉 기본 레이아웃 그리드에 미리 정의 된 색상을 설정하지 않고 수행해야합니다. 이 방법을 구현할 수 있습니까? – TecMan

+0

BTW, 그리드 노드의 간단한 속성 설정조차도 도움이됩니다 :''. – TecMan

+0

그리드의 기본 배경을 정의 할 수 있습니다. 기본 제공 기본값은 투명 또는 흰색이어야합니다. 다시 설정하면 더 일반적인 접근 방식을 갖게됩니다. – lokusking