2017-09-23 1 views
0

두 개의 상태 중 하나를 Big 및 다른 Small로 정의했으며 큰 상태는 창 크기를 늘리고 작 으면 창 크기를 줄입니다.VisualStateManager는 wpf 양식의 윈도우 크기를 변경하지 않습니다.

창에는 탭 컨트롤과 2 개의 tabitem이 있습니다. 내가하고 싶은 일은, tab1이 선택 될 때 Big state로 window의 크기를 변경하고 tab2가 선택 될 때 Window의 크기를 Small 상태로 변경하는 것입니다. 다음 코드를 작성했지만 작동하지 않습니다.

다음은 XAML 코드입니다. 다음

<Window x:Name="window" x:Class="WpfApp2.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:WpfApp2" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid> 
    <VisualStateManager.VisualStateGroups> 
     <VisualStateGroup x:Name="VisualStateGroup"> 
      <VisualStateGroup.Transitions> 
       <VisualTransition From="Big" GeneratedDuration="0:0:1" To="Small"/> 
       <VisualTransition From="Small" GeneratedDuration="0:0:1" To="Big"/> 
      </VisualStateGroup.Transitions> 
      <VisualState x:Name="Big"> 
       <Storyboard> 
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="window"> 
         <EasingDoubleKeyFrame KeyTime="0" Value="558.333"/> 
        </DoubleAnimationUsingKeyFrames> 
       </Storyboard> 
      </VisualState> 
      <VisualState x:Name="Small"> 
       <Storyboard> 
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="window"> 
         <EasingDoubleKeyFrame KeyTime="0" Value="290.152"/> 
        </DoubleAnimationUsingKeyFrames> 
       </Storyboard> 
      </VisualState> 
     </VisualStateGroup> 
    </VisualStateManager.VisualStateGroups> 
    <TabControl HorizontalAlignment="Left" Height="319" VerticalAlignment="Top" Width="517" SelectionChanged="changes"> 
     <TabItem Header="TabItem" Name="tab1"/> 
     <TabItem Header="TabItem" Name="tab2"/> 
    </TabControl> 

</Grid> 

는 C# 코드입니다.

private void changes(object sender, SelectionChangedEventArgs e) 
    { 
     if (tab1.IsSelected) 
     { 
      VisualStateManager.GoToState(window, "Big", true); 
     } 
     else 
     { 
      VisualStateManager.GoToState(window, "Small", true); 
     } 
     i++; 
    } 

답변

0

는 먼저 오른쪽 루트 요소합니다 (Window) 아래 VisualStateManager.VisualStateGroups 부착 성 및 내용을 이동한다.

<Window x:Name="window" x:Class="WpfApp2.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:WpfApp2" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="350" Width="525"> 
    <VisualStateManager.VisualStateGroups> 
     <VisualStateGroup x:Name="VisualStateGroup"> 
      <VisualStateGroup.Transitions> 
       <VisualTransition From="Big" GeneratedDuration="0:0:1" To="Small"/> 
       <VisualTransition From="Small" GeneratedDuration="0:0:1" To="Big"/> 
      </VisualStateGroup.Transitions> 
      <VisualState x:Name="Big"> 
       <Storyboard> 
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="window"> 
         <EasingDoubleKeyFrame KeyTime="0" Value="558.333"/> 
        </DoubleAnimationUsingKeyFrames> 
       </Storyboard> 
      </VisualState> 
      <VisualState x:Name="Small"> 
       <Storyboard> 
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="window"> 
         <EasingDoubleKeyFrame KeyTime="0" Value="290.152"/> 
        </DoubleAnimationUsingKeyFrames> 
       </Storyboard> 
      </VisualState> 
     </VisualStateGroup> 
    </VisualStateManager.VisualStateGroups> 

    <Grid> 
     <TabControl HorizontalAlignment="Left" Height="319" VerticalAlignment="Top" Width="517" SelectionChanged="changes"> 
      <TabItem Header="TabItem" Name="tab1"/> 
      <TabItem Header="TabItem" Name="tab2"/> 
     </TabControl> 

    </Grid> 
</Window> 

대신 GoToStateGoToElementState 방법을 사용합니다. ControlTemplate 바깥쪽에는 GoToElementState 메서드를 사용해야합니다.

private void changes(object sender, SelectionChangedEventArgs e) 
{ 
    if (tab1.IsSelected) 
    { 
     VisualStateManager.GoToElementState(window, "Big", true); 
    } 
    else 
    { 
     VisualStateManager.GoToElementState(window, "Small", true); 
    } 
} 
관련 문제