2017-09-23 111 views
0

我已經定義了2個狀態一個Big和其他Small和大的狀態將增加窗口大小和小將減少窗口大小。VisualStateManager不會改變wpf窗口的窗口大小

該窗口有一個選項卡控件和2個tabitem。我想要做的是在選擇tab1時將窗口大小更改爲Big狀態,並在選擇tab2時將窗口大小更改爲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> 

而且使用GoToElementState方法,而不是GoToState。在ControlTemplate之外,應該使用GoToElementState方法。

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