2010-03-07 85 views
3

我在我的主窗口的LayoutRoot電網定義了兩個可視狀態如下:WPF - 從用戶控件操控的VisualState

<Grid x:Name="LayoutRoot" Background="#FF434343" ShowGridLines="True"> 

<Grid.RowDefinitions> 
    <RowDefinition Height="100"/> 
    <RowDefinition Height="*"/> 
</Grid.RowDefinitions> 


    <VisualStateManager.CustomVisualStateManager> 
     <ic:ExtendedVisualStateManager/> 
    </VisualStateManager.CustomVisualStateManager> 
    <VisualStateManager.VisualStateGroups> 
     <VisualStateGroup x:Name="VisualStateLogin"> 
      <VisualStateGroup.Transitions> 
       <VisualTransition GeneratedDuration="00:00:00.6000000"/> 
      </VisualStateGroup.Transitions> 
      <VisualState x:Name="LoggedOn"> 
       <Storyboard> 
        <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="{x:Null}" Storyboard.TargetProperty="(Window.WindowStyle)"> 
         <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static WindowStyle.None}"/> 
        </ObjectAnimationUsingKeyFrames> 
        <BooleanAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="{x:Null}" Storyboard.TargetProperty="(Window.AllowsTransparency)"> 
         <DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="True"/> 
        </BooleanAnimationUsingKeyFrames> 
       </Storyboard> 
      </VisualState> 
      <VisualState x:Name="LoggedOff"> 
       <Storyboard> 
        <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="brdContent" Storyboard.TargetProperty="(UIElement.Opacity)"> 
         <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0.1"/> 
        </DoubleAnimationUsingKeyFrames> 
        <BooleanAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="brdContent" Storyboard.TargetProperty="(UIElement.IsEnabled)"> 
         <DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="False"/> 
        </BooleanAnimationUsingKeyFrames> 
        <BooleanAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="ucTabButtons" Storyboard.TargetProperty="(UIElement.IsEnabled)"> 
         <DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="False"/> 
        </BooleanAnimationUsingKeyFrames> 
        <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="ucTabButtons" Storyboard.TargetProperty="(UIElement.Opacity)"> 
         <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0.4"/> 
        </DoubleAnimationUsingKeyFrames> 
       </Storyboard> 
      </VisualState> 
     </VisualStateGroup> 
    </VisualStateManager.VisualStateGroups> 

它工作正常開關從一個狀態到另一個狀態thoughtout的主窗口隱藏代碼,這樣的:

void MainWindow_Loaded(object sender, RoutedEventArgs e) 
    { 
     //ExtendedVisualStateManager.GoToElementState(this.LayoutRoot as FrameworkElement, "LoggedOff", false);   
    } 

但現在,我需要能夠從主窗口內的用戶控件開關狀態......

我已經tryed說:

private void btnOk_Click(object sender, System.Windows.RoutedEventArgs e) 
    { 
     MainWindow parent = new MainWindow(); 
     Grid root = new Grid(); 
     root = (Grid)parent.FindName("LayoutRoot"); 
     ExtendedVisualStateManager.GoToElementState(root as FrameworkElement, "LoggedOn", true); 
    } 

,這讓我以下異常:

System.NullReferenceException被未處理 消息=「對象引用不設置爲一個對象的一個​​實例。」 來源=「WPFToolkit」

有人知道如何切換從usercontrol狀態?

THANKYOU,

Josi

回答

1

我推薦定義的接口狀態之間的切換。表單可以實現接口,它可以顯式地將接口傳遞給控件,​​或者控件可以查詢它的父接口以查看接口是否被實現。這樣,用戶控件就不會緊密地耦合到它的容器,而僅僅是它期望容器提供的行爲。

在一個側面說明,有一個用戶控件修改其容器的狀態是一個不尋常的設計。您可能想考慮是否有另外一種方法來構建需求。

一個可能的接口:

public interface IStateChanger 
{ 
    void GoToElementState(string state);  
} 

你可以有你的主窗口實現這個接口:

void IStateChanger.GoToElementState(string state) 
    { 
     ExtendedVisualStateManager.GoToElementState(this.LayoutRoot as FrameworkElement, state, false);   
    } 

然後你的控制,下面的例子中,可以這樣做:

private void btnOk_Click(object sender, System.Windows.RoutedEventArgs e) 
    { 
     MainWindow parent = new MainWindow(); 
     if (parent is IStateChanger) 
      ((IStateChanger)parent).GoToElementState("LoggedOn"); 
    } 
0

在父母可以訂閱的用戶控件上創建一個公共事件;該事件可以被用作狀態轉換的觸發器。