2013-04-12 5 views
2

컨트롤 템플릿 내에서 VisualStates를 정의하는 경우 스토리 보드에서 템플릿 컨트롤 자체의 속성을 변경할 수 있습니까? 다음은 간단한 예입니다.VisualState에서 템플릿 컨트롤의 속성을 변경하는 방법?

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Window.Template> 
    <ControlTemplate TargetType="{x:Type Window}"> 
     <Grid> 
     <VisualStateManager.VisualStateGroups> 
      <VisualStateGroup x:Name="WindowStyleStates" 
          x:Uid="WindowStyleStates"> 
      <Storyboard x:Uid="Storyboard_1"> 
       <ObjectAnimationUsingKeyFrames Storyboard.TargetName="?????" 
              Storyboard.TargetProperty="ResizeMode"> 
       <DiscreteObjectKeyFrame KeyTime="0" 
             Value="CanResizeWithGrip" /> 
       </ObjectAnimationUsingKeyFrames> 
      </Storyboard> 
      </VisualStateGroup> 
     </VisualStateManager.VisualStateGroups> 
     </Grid> 
    </ControlTemplate> 
    </Window.Template> 
</Window> 

문제는 스토리 보드는 그리드 내에 정의 된 개체에만 액세스 할 수 있다는 것입니다. Window에 대한 controltemplate을 정의하고 있다면 왜 템플릿을 만들고있는 Window에서 값을 변경할 수 없습니까?

답변

1

VisualStateManager에 액세스하려면 ControlTemplate이 필요하지 않습니다. 비록 시도하지는 않았지만,이 트릭을해야합니다.

<Window x:Name="YourWindow" ...> 
    <VisualStateManager.VisualStateGroups> 
     <VisualStateGroup x:Name="WindowStyleStates" x:Uid="WindowStyleStates"> 
      <Storyboard x:Uid="Storyboard_1"> 
       <ObjectAnimationUsingKeyFrames Storyboard.TargetName="YourWindow" 
               Storyboard.TargetProperty="ResizeMode"> 
        <DiscreteObjectKeyFrame KeyTime="0" Value="CanResizeWithGrip"/> 
       </ObjectAnimationUsingKeyFrames> 
      </Storyboard> 
     </VisualStateGroup> 
    </VisualStateManager.VisualStateGroups> 
</Window> 

는하지만 적어도 닷넷 3.5, 뒤에 코드에서 VisualStateManager.GoToState(...)에 문제가있는 것 같습니다. 그러나 workaround이 있습니다. 이것이 당신에게 중요한지 나는 모른다.

제목에있는 질문에 대답하려면 : 나는 컨트롤의 템플리트 뒤에있는 약간의 개념을 잘못 이해했다고 생각합니다. 작은 예제 ...

<!-- A simple button with round corners --> 
<Button> 
    <Button.Template> 
    <ControlTemplate> 
     <Border x:Name="ButtonBorder" 
       CornerRadius="10" 
       BorderBrush="{TemplateBinding BorderBrush}" 
       BorderThickness="{TemplateBinding BorderThickness}"> 
     <Grid> 
      <ContentPresenter x:Name="ButtonContent" 
          Content="{TemplateBinding Content}" /> 
      <Border x:Name="ButtonBackground" 
        Background="{TemplateBinding Background}" 
        BorderBrush="{x:Null}" 
        BorderThickness="0" /> 
     </Grid> 
     </Border> 
    </ControlTemplate> 
    </Button.Template> 
</Button> 

템플릿에서 알 수 있듯이 Button은 새로운 모양을 제공합니다. 또한 시각적 동작도 무시됩니다. 단추가 예상대로 작동하지만이 경우에는 시각적 인 동작이 없습니다.
시각적 동작을 정의하려면 VisualStateManager과 미리 정의 된 상태 또는 사용자 지정 상태를 사용할 수 있습니다. 그러나 버튼의 모양과 느낌을 다시 구현하기 때문에 템플릿의 요소 만 수정할 수 있습니다. 따라서 을 ButtonBorder에 추가하십시오.

+0

시각적 상태를 제어 템플릿 밖으로 이동하면 부분적으로 템플릿 창이 속성이 수정됩니다. Expression Blend는 컨트롤 자체에 배치 된 상태를 확인하지 않기 때문에 선호하는 메서드라고 생각하지 않습니다. –

관련 문제