Silverlight DataStateBehavior,未使用初始值
我正在尝试使用 Silverlight DataStateBehavior,在大多数情况下,当我单击一个将视图模型中的“选定”属性设置为 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 中看到任何更改,直到我单击按钮来更改视图模型属性。
我知道有几个类涉及 DataState 内容,包括:
- BindingListener.cs
- ConverterHelper.cs
- DataStateBehavior.cs
- DataStateSwitchBehavior.cs
- DataTrigger.cs
任何线索都会很好, 谢谢
I am trying to use the Silverlight DataStateBehavior, it works fine in most cases where I click a button which sets say a 'Selected' Property in the view model to either false or true. The DataStateBehavior then tells the VisualStateManager to go to the relevant state.
Like this:
<Button...>
<i:Interaction.Behaviors>
<id:DataStateBehavior Binding="{Binding Selected}" Value="True" TrueState="SelectedVisualState" FalseState="DeselectedVisualState"/>
</i:Interaction.Behaviors>
</Button>
The above works fine. What I am trying to do though is to get it to set the correct state when the application loads, if I were to set the 'Selected' property on the view model to true by default I wouldn't see any changes in the UI until I clicked the button to change the viewmodel property.
I know there are several classes involved with the DataState stuff including:
- BindingListener.cs
- ConverterHelper.cs
- DataStateBehavior.cs
- DataStateSwitchBehavior.cs
- DataTrigger.cs
Any clues would be good,
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上,我要添加我刚刚尝试过的第二个答案,它似乎更清晰,因为它可以在 XAML 中完成,并且无需自定义行为。我将留下另一个答案作为替代解决方案的参考,因为它们都有效。
您只需要添加对 Microsoft.Expression.Interactions 程序集的引用,该程序集是 Blend SDK 的一部分。
I'm actually going to add a second answer which I just tried, and seems to be cleaner since it can be done all in XAML and without a custom behavior. I'll leave the other answer just as a reference for an alternative solution since they both work.
You will just need at add a reference to the Microsoft.Expression.Interactions assembly that is part of the Blend SDK.
尝试 DataStateBehavior 类的此扩展。当目标元素加载时,将评估 DataStateBehavior,就像属性已更新一样。
Try this extension of the DataStateBehavior class. When the target element loads, the DataStateBehavior will be evaluate as if the property has been updated.
我解决这个问题的一种方法是创建一种可以添加到控件中的行为,以在加载时将其置于初始视觉状态。这是一个简单的示例:
然后,您只需将该行为添加到 XAML 中的 UserControl 级别:
您还可以轻松修改它以接受逗号分隔的初始状态列表,然后如果您需要将加载后控制一堆不同的互斥状态。
这也可能被重构为 TriggerAction,您可以触发控件的 Loaded 事件,我不确定哪种方式会更干净。
One way I have solved this problem is to make a behavior you can add to your control to put it into an initial visual state upon loading. Here is a simple example:
You would then just add that behavior to the UserControl-level in XAML:
You could also easily modify this to accept a comma separated list of initial states which you could then split and loop through if you needed to put the control in a bunch of different mutually exclusive states after loading.
This might also be refactored into a TriggerAction that you could just trigger off of the Loaded event of the control, I'm not sure which way would be cleaner.