有没有可能有嵌套的视觉状态。我的意思是,如果一个ParentControl有一个ChildControl,并且两者都有自己的可视状态,那么是否可以通过设置ParentControl的状态来相应地更改ChildControl的状态。
发布于 2010-01-28 23:04:27
您需要调用GoToState方法来更改子控件的可视状态。
因为你需要调用一个方法,所以你不能在父控件的可视化状态管理器中使用序列图像板,因为它们只能动画显示属性。
因此,您需要在子控件中编写一些代码。来监视父级的状态并做出适当的响应。
有许多不同的方法可以做到这一点,但关键的信息是使用VisualStateManager.GetVisualStateGroups方法找到您感兴趣的父级上的VisualStateGroup,然后附加到该组的CurrentStateChanging事件。因此,当父控件将其感兴趣的状态转换到某个状态时,可以通知该子控件中的代码,并且可以针对其自身适当地调用GoToState。
发布于 2010-01-29 01:18:47
我只需要声明一个新的依赖属性:
public static readonly DependencyProperty StateProperty =
DependencyProperty.Register("State",
typeof( string ),
typeof( TextBlockControl ),
new PropertyMetadata("Top",
new PropertyChangedCallback(StateChanged)));
[Category("DigItOut"), Description("State")]
public string State
{
get
{
return this.GetValue(StateProperty).ToString();
}
set
{
this.SetValue(StateProperty, value);
}
}
private static void StateChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
if (!String.IsNullOrEmpty(args.NewValue.ToString()))
VisualStateManager.GoToState(sender as TextBlockControl, args.NewValue.ToString(), true);
}然后从它的parents状态设置它:
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="States">
<VisualState x:Name="Reverse">
<Storyboard>
<ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="textBlockControl" Storyboard.TargetProperty="(TextBlockControl.State)">
<DiscreteObjectKeyFrame KeyTime="00:00:00">
<DiscreteObjectKeyFrame.Value>
<System:String>Bottom</System:String>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Straight"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>但是,如果我仍然希望控制转换的使用,那么我必须找到另一个解决方案。可能是第二处房产。
https://stackoverflow.com/questions/2154936
复制相似问题