VisualStateManager.GotoState 在 ChildWindow 中不起作用

发布于 2024-10-27 19:55:28 字数 7139 浏览 0 评论 0原文

我的 silverlight 4 应用程序中有一个 ChildWindow,定义了 2 个视觉状态。

当我在 Blend 中预览它们时,视觉状态会执行我想要的操作,但是当我调用 ViewStateManager.GotoState 时,它​​会返回 false 并且视觉状态不会转换。

这是我的 XAML。抱歉长度...

<controls:ChildWindow 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:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" 
                      xmlns:Controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input" 
                      xmlns:Controls1="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" 
                      xmlns:Interactivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
                      xmlns:Behaviors="clr-namespace:Civica.UI.Common.Behaviors;assembly=Civica.UI.Common"
                      xmlns:Controls2="clr-namespace:Civica.UI.Common.Controls;assembly=Civica.UI.Common" 
                      xmlns:Helpers="clr-namespace:Civica.UI.Common.Helpers;assembly=Civica.UI.Common" 
                      x:Class="Civica.UI.Administration.Staff.StaffSearch.StaffSearchView"
                      mc:Ignorable="d"
                      d:DesignHeight="300" d:DesignWidth="400"
                      Width="800"
                      Helpers:VisualStateHelper.VisualStateName="{Binding Path=VisualStateName}">

    <controls:ChildWindow.Title>
        <TextBlock Text="Staff Search" Margin="0,4" TextWrapping="Wrap" />
    </controls:ChildWindow.Title>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="SearchResultsStateGroup">
                <VisualState x:Name="NoSearchResults">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="border">
                            <DiscreteObjectKeyFrame KeyTime="0">
                                <DiscreteObjectKeyFrame.Value>
                                    <Visibility>Visible</Visibility>
                                </DiscreteObjectKeyFrame.Value>
                            </DiscreteObjectKeyFrame>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="textBlock">
                            <DiscreteObjectKeyFrame KeyTime="0">
                                <DiscreteObjectKeyFrame.Value>
                                    <Visibility>Visible</Visibility>
                                </DiscreteObjectKeyFrame.Value>
                            </DiscreteObjectKeyFrame>
                        </ObjectAnimationUsingKeyFrames>
                        <DoubleAnimation Duration="0" To="0.3" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="dataGrid" d:IsOptimized="True"/>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <Grid Margin="0,0,0,7">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto "/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Controls:Label Content="Search for:" Margin="0,0,10,0"/>

            <Controls2:CommandTextBox Grid.Row="1" Grid.Column="1" 
                                      ImageHeight="16" ImageWidth="16"
                                      Text="{Binding CurrentSearchTerm, Mode=TwoWay}"
                                      Command="{Binding SearchCommand}" 
                                      CommandParameter="{Binding CurrentSearchTerm}"
                                      Image="/Civica.UI.Common.Images;component/Resources/Icons/16x16/RGB/icon-search-tiny.png" />
        </Grid>

        <Grid Grid.Row="1">
            <Controls1:DataGrid x:Name="dataGrid" AutoGenerateColumns="False" 
                                IsReadOnly="True"
                                AreRowGroupHeadersFrozen="True" 
                                CanUserResizeColumns="True" 
                                ItemsSource="{Binding CurrentSearchResults}"
                                SelectedItem="{Binding CurrentlySelectedSearchResult, Mode=TwoWay}"
                                VerticalScrollBarVisibility="Auto"
                                Height="300">
                <Interactivity:Interaction.Behaviors>
                    <Behaviors:DataGridDoubleClickBehaviour DoubleClickCommand="{Binding SelectSearchResultCommand}" 
                                                            CommandParameter="{Binding CurrentlySelectedSearchResult}"/>
                </Interactivity:Interaction.Behaviors>
                <Controls1:DataGrid.Columns>
                    <Controls1:DataGridTextColumn Header="Name" Binding="{Binding DisplayName}" IsReadOnly="True" />
                    <Controls1:DataGridTextColumn Header="Phone" Binding="{Binding TelephoneNumber}" IsReadOnly="True" />
                    <Controls1:DataGridTextColumn Header="Location" Binding="{Binding Location}" IsReadOnly="True" />
                </Controls1:DataGrid.Columns>
            </Controls1:DataGrid>
            <TextBlock x:Name="textBlock" Text="No Results Found" Foreground="Black" Opacity="1" FontSize="13" VerticalAlignment="Top" HorizontalAlignment="Center" Margin="30" Visibility="Collapsed"/>
            <Border x:Name="border" Background="Black" Opacity="0.3" Visibility="Collapsed"/>
        </Grid>        
    </Grid>
</controls:ChildWindow>

VisualStateHelper.VisualStateName 只是一个附加属性,当值更改时调用 VisualStateManager。该属性的实施可能不是问题,因为它在其他地方也有效。

private static void OnVisualStateNameChanged(object sender, DependencyPropertyChangedEventArgs args)
{
     var visualStateName = (string)args.NewValue;
     var control = sender as Control;
     if (control == null)
     {
        throw new ArgumentException(Resources.VisualStateHelperSenderNotAControlErrorMessage, "sender");
     }

     VisualStateManager.GoToState(control, visualStateName, true);
}

I've got a ChildWindow in my silverlight 4 application with 2 visual states defined.

The visual states do what I want to when I preview them in Blend but when I call ViewStateManager.GotoState it returns false and the visual state isn't transitioned.

Heres my XAML. Appologies for the length...

<controls:ChildWindow 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:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" 
                      xmlns:Controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input" 
                      xmlns:Controls1="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" 
                      xmlns:Interactivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
                      xmlns:Behaviors="clr-namespace:Civica.UI.Common.Behaviors;assembly=Civica.UI.Common"
                      xmlns:Controls2="clr-namespace:Civica.UI.Common.Controls;assembly=Civica.UI.Common" 
                      xmlns:Helpers="clr-namespace:Civica.UI.Common.Helpers;assembly=Civica.UI.Common" 
                      x:Class="Civica.UI.Administration.Staff.StaffSearch.StaffSearchView"
                      mc:Ignorable="d"
                      d:DesignHeight="300" d:DesignWidth="400"
                      Width="800"
                      Helpers:VisualStateHelper.VisualStateName="{Binding Path=VisualStateName}">

    <controls:ChildWindow.Title>
        <TextBlock Text="Staff Search" Margin="0,4" TextWrapping="Wrap" />
    </controls:ChildWindow.Title>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="SearchResultsStateGroup">
                <VisualState x:Name="NoSearchResults">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="border">
                            <DiscreteObjectKeyFrame KeyTime="0">
                                <DiscreteObjectKeyFrame.Value>
                                    <Visibility>Visible</Visibility>
                                </DiscreteObjectKeyFrame.Value>
                            </DiscreteObjectKeyFrame>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="textBlock">
                            <DiscreteObjectKeyFrame KeyTime="0">
                                <DiscreteObjectKeyFrame.Value>
                                    <Visibility>Visible</Visibility>
                                </DiscreteObjectKeyFrame.Value>
                            </DiscreteObjectKeyFrame>
                        </ObjectAnimationUsingKeyFrames>
                        <DoubleAnimation Duration="0" To="0.3" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="dataGrid" d:IsOptimized="True"/>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <Grid Margin="0,0,0,7">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto "/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Controls:Label Content="Search for:" Margin="0,0,10,0"/>

            <Controls2:CommandTextBox Grid.Row="1" Grid.Column="1" 
                                      ImageHeight="16" ImageWidth="16"
                                      Text="{Binding CurrentSearchTerm, Mode=TwoWay}"
                                      Command="{Binding SearchCommand}" 
                                      CommandParameter="{Binding CurrentSearchTerm}"
                                      Image="/Civica.UI.Common.Images;component/Resources/Icons/16x16/RGB/icon-search-tiny.png" />
        </Grid>

        <Grid Grid.Row="1">
            <Controls1:DataGrid x:Name="dataGrid" AutoGenerateColumns="False" 
                                IsReadOnly="True"
                                AreRowGroupHeadersFrozen="True" 
                                CanUserResizeColumns="True" 
                                ItemsSource="{Binding CurrentSearchResults}"
                                SelectedItem="{Binding CurrentlySelectedSearchResult, Mode=TwoWay}"
                                VerticalScrollBarVisibility="Auto"
                                Height="300">
                <Interactivity:Interaction.Behaviors>
                    <Behaviors:DataGridDoubleClickBehaviour DoubleClickCommand="{Binding SelectSearchResultCommand}" 
                                                            CommandParameter="{Binding CurrentlySelectedSearchResult}"/>
                </Interactivity:Interaction.Behaviors>
                <Controls1:DataGrid.Columns>
                    <Controls1:DataGridTextColumn Header="Name" Binding="{Binding DisplayName}" IsReadOnly="True" />
                    <Controls1:DataGridTextColumn Header="Phone" Binding="{Binding TelephoneNumber}" IsReadOnly="True" />
                    <Controls1:DataGridTextColumn Header="Location" Binding="{Binding Location}" IsReadOnly="True" />
                </Controls1:DataGrid.Columns>
            </Controls1:DataGrid>
            <TextBlock x:Name="textBlock" Text="No Results Found" Foreground="Black" Opacity="1" FontSize="13" VerticalAlignment="Top" HorizontalAlignment="Center" Margin="30" Visibility="Collapsed"/>
            <Border x:Name="border" Background="Black" Opacity="0.3" Visibility="Collapsed"/>
        </Grid>        
    </Grid>
</controls:ChildWindow>

VisualStateHelper.VisualStateName is just an attached property that calls the VisualStateManager when the value is changed. The implementation of the property is probably not the problem as it works elsewhere.

private static void OnVisualStateNameChanged(object sender, DependencyPropertyChangedEventArgs args)
{
     var visualStateName = (string)args.NewValue;
     var control = sender as Control;
     if (control == null)
     {
        throw new ArgumentException(Resources.VisualStateHelperSenderNotAControlErrorMessage, "sender");
     }

     VisualStateManager.GoToState(control, visualStateName, true);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纸伞微斜 2024-11-03 19:55:28

我对 Silverlight 还很陌生,但我在 Google 上发现了这个,它看起来解决了您遇到的问题:

将视觉状态管理器与 Silverlight Toolkit 的子 Windows 控件结合使用

I'm pretty new to Silverlight but I found this on Google which looks like it resolves the issue you're having:

Using Visual State Manager with Silverlight Toolkit’s Child Windows control

荭秂 2024-11-03 19:55:28

它不适用于 ChildWindow,但适用于 UserControl 或普通页面

VisualStateManager.GoToState(this, "MyState", true);

解决方案是在 ChildWindow 中时使用下面的代码

ExtendedVisualStateManager.GoToElementState(this.LayoutRoot as FrameworkElement, "MyState", true);

谢谢

it doesn't works in a ChildWindow, but works in a UserControl or Normal Page

VisualStateManager.GoToState(this, "MyState", true);

The solution is use the code below when you be in a ChildWindow

ExtendedVisualStateManager.GoToElementState(this.LayoutRoot as FrameworkElement, "MyState", true);

Thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文