none
如何在wpf 中实现 在 xaml 中 gotoVisualElementState? RRS feed

  • 问题

  •  <RadioButton x:Name="thumb3" Height="130" Style="{StaticResource Thumb3_RadioButtonStyle}" Width="109" Canvas.Left="25" Canvas.Top="314" Content="RadioButton" Cursor="Hand">
                                    <System_Windows_Interactivity:Interaction.Triggers>
                                        <System_Windows_Interactivity:EventTrigger EventName="Checked">
                                            <PCGaming:GoToUserControlState StateName="ShowFeature3"/>
                                        </System_Windows_Interactivity:EventTrigger>
                                        <System_Windows_Interactivity:EventTrigger EventName="MouseEnter">
                                            <im:PlaySoundAction Source="Audio/overGeneral.mp3"/>
                                        </System_Windows_Interactivity:EventTrigger>
                                        <System_Windows_Interactivity:EventTrigger EventName="Click">
                                            <im:PlaySoundAction Source="Audio/clickMenu.mp3"/>
                                        </System_Windows_Interactivity:EventTrigger>
                                    </System_Windows_Interactivity:Interaction.Triggers>
                                </RadioButton>

    // This action searches the visual tree (parent elements) looking for
     // states defined in the topmost UserControl (MainPage).
     // This way one can trigger a UserControl state from inside any template.
        public class GoToUserControlState : TriggerAction<DependencyObject>
        {
            #region Property to Expose

      public static readonly DependencyProperty StateNameProperty = DependencyProperty.Register("State Name", typeof(string), typeof(GoToUserControlState), null);

      [Category("State Properties")]
            public string StateName
            {
                get { return (string)GetValue(StateNameProperty); }
                set { SetValue(StateNameProperty, value); }
            }

            #endregion

            protected override void Invoke(object o)
            {
       DependencyObject parent = this.AssociatedObject as DependencyObject;
       while (parent != null)
       {
                    if (parent.GetType() == typeof(MainPage))
                    {
                        VisualStateManager.GoToState(parent as Control, StateName, true);
                        break;
                    }
        parent = VisualTreeHelper.GetParent(parent) as DependencyObject;
       }
            }
        }

    silverlight 实现效果如上。

    不借助 interactivity (方便后期移植)如何实现这种方式呢?(xaml中进行visualState跳转)

    2012年6月30日 8:49

答案

  • WPF相对于Metro的就比较简单了,因为我们可以通过Expression SDK来引入 Interactivity。

    引入 System.Windows.Interactivity 程序集,就可以使用了:

    <Window x:Class="WpfApplication3.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:local="clr-namespace:WpfApplication3" Title="MainWindow" Height="350" Width="525"> <Grid> <Button> <i:Interaction.Triggers> <i:EventTrigger> <local:GoToUserControlState StateName="Custom"/> </i:EventTrigger> </i:Interaction.Triggers> </Button> ....



    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    2012年7月2日 10:33
    版主

全部回复

  •  <System_Windows_Interactivity:EventTrigger EventName="Checked">
                                            <PCGaming:GoToUserControlState StateName="ShowFeature3"/>
                                        </System_Windows_Interactivity:EventTrigger>

    补充一下。类似这种写法

    2012年6月30日 8:50
  • WPF相对于Metro的就比较简单了,因为我们可以通过Expression SDK来引入 Interactivity。

    引入 System.Windows.Interactivity 程序集,就可以使用了:

    <Window x:Class="WpfApplication3.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:local="clr-namespace:WpfApplication3" Title="MainWindow" Height="350" Width="525"> <Grid> <Button> <i:Interaction.Triggers> <i:EventTrigger> <local:GoToUserControlState StateName="Custom"/> </i:EventTrigger> </i:Interaction.Triggers> </Button> ....



    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    2012年7月2日 10:33
    版主
  • 这是一个很不错的办法,我的初衷是 让wpf开发的程序能很好的移植到metro  所以想要尽量保证 metro能使用的写法。 如您之前在metro 板块回复的方法。


    非常荣幸成为.net 开发者

    2012年7月2日 10:40