我正在尝试使用Silverlight DataStateBehavior,它在大多数情况下工作正常,我单击一个按钮,将视图模型中的“Selected”属性设置为false或true。然后,DataStateBehavior告诉VisualStateManager进入相关状态。
像这样:
<Button...>
<i:Interaction.Behaviors>
<id:DataStateBehavior Binding="{Binding Selected}" Value="True" TrueState="SelectedVisualState" FalseState="DeselectedVisualState"/>
</i:Interaction.Behaviors>
</Button>
以上工作正常。我试图做的是让它在应用程序加载时设置正确的状态,如果我将视图模型上的'Selected'属性设置为true,默认情况下我不会看到UI中的任何更改我单击按钮更改viewmodel属性。
我知道有几个涉及DataState的类,包括:
任何线索都会很好, 感谢
答案 0 :(得分:3)
我实际上要添加我刚刚尝试的第二个答案,并且似乎更清晰,因为它可以在XAML中完成并且没有自定义行为。我将留下另一个答案作为替代解决方案的参考,因为它们都有效。
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<ic:GoToStateAction StateName="SelectedVisualState"/>
</i:EventTrigger>
</i:Interaction.Triggers>
您只需添加对作为Blend SDK一部分的Microsoft.Expression.Interactions程序集的引用。
xmlns:ic="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"
答案 1 :(得分:2)
尝试DataStateBehavior类的此扩展。加载目标元素时,将评估DataStateBehavior,就好像属性已更新一样。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
namespace Gusdor.Wpf
{
/// <summary>
/// Fix for data state behavior. Behavior will trigger transitions when target element loads.
/// </summary>
class DataStateBehaviorFix: Microsoft.Expression.Interactivity.Core.DataStateBehavior
{
public bool UseTransitionsOnLoad { get; set; }
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.Loaded += AssociatedObject_Loaded;
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.Loaded -= AssociatedObject_Loaded;
}
void AssociatedObject_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
Evaluate();
}
void Evaluate()
{
if (Value == null)
{
GotoState(Binding == null, this.AssociatedObject);
}
else GotoState(Value.Equals(Binding), this.AssociatedObject);
}
/// <summary>
/// Attempts to change to the named state. Walks up tree to first match.
/// </summary>
/// <param name="flag"></param>
/// <param name="element"></param>
void GotoState(bool flag, FrameworkElement element)
{
string stateName = flag ? TrueState : FalseState;
if (HasState(element, stateName))
{
bool ret = System.Windows.VisualStateManager.GoToElementState(element, stateName, UseTransitionsOnLoad);
}
else if (element.Parent as FrameworkElement != null)
GotoState(flag, element.Parent as FrameworkElement);
}
/// <summary>
/// Checks if an element has the state named
/// </summary>
/// <param name="element"></param>
/// <param name="stateName"></param>
/// <returns></returns>
bool HasState(FrameworkElement element, string stateName)
{
var groups = Microsoft.Expression.Interactivity.VisualStateUtilities.GetVisualStateGroups(element).Cast<VisualStateGroup>();
return groups.Any(p => p.States.Cast<VisualState>().Any(s => s.Name == stateName));
}
}
}
答案 2 :(得分:1)
我解决此问题的一种方法是创建一个可以添加到控件中的行为,以便在加载时将其置于初始可视状态。这是一个简单的例子:
public class InitialVisualStateBehavior : Behavior<Control>
{
public static readonly DependencyProperty InitialStateProperty = DependencyProperty.Register(
"InitialState",
typeof(string),
typeof(InitialVisualStateBehavior),
null);
public string InitialState
{
get { return (string)GetValue(InitialStateProperty); }
set { SetValue(InitialStateProperty, value); }
}
protected override void OnAttached()
{
base.OnAttached();
if (this.AssociatedObject != null)
{
this.AssociatedObject.Loaded += new RoutedEventHandler(AssociatedObject_Loaded);
}
}
protected override void OnDetaching()
{
base.OnDetaching();
if (this.AssociatedObject != null)
{
this.AssociatedObject.Loaded -= AssociatedObject_Loaded;
}
}
private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
{
VisualStateManager.GoToState(this.AssociatedObject, this.InitialState, false);
}
}
然后,您只需将该行为添加到XAML中的UserControl级别:
<i:Interaction.Behaviors>
<myi:InitialVisualStateBehavior InitialState="SelectedVisualState" />
</i:Interaction.Behaviors>
您也可以轻松地修改它以接受逗号分隔的初始状态列表,如果您需要在加载后将控件置于一堆不同的互斥状态,则可以拆分并循环。
这也可以重构为TriggerAction,你可以触发控件的Loaded事件,我不知道哪种方式会更清晰。