У меня есть приложение ChildWindow в моем приложении silverlight 4 с определенными 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);
}