Windows Store应用程序上的条件样式(或Style.Triggers-> DataTrigger等效)的最佳做法? [英] Best practice for conditional styling (or Style.Triggers->DataTrigger equivalent) on Windows Store app?
问题描述
我已经用尽我的互联网搜索,似乎找不到根据数据绑定条件设计我的Windows Store App XAML元素的最佳做法?
I have exhausted my internet searches and cannot seem to find the best practice on styling my Windows Store App XAML elements based on data binding conditions?
< Style.Triggers>< DataTrigger> ...< / DataTrigger>< /Style.Triggers>
似乎在Windows 8 Store App似乎不可用WPF和Visual State Manager仅用于预先设置的交互状态,例如 MouseOver
是不是?如何根据我的底层视图模型来显着改变我的UI?
<Style.Triggers><DataTrigger>...</DataTrigger></Style.Triggers>
doesn't appear to be available on Windows 8 Store Apps like it was in WPF, and the Visual State Manager is just for pre-set interaction states such as MouseOver
is it not? How can I dramatically change my UI depending on my underlying View Model?
为了创建一个明确的答案,这个问题的方案是什么,最普遍的接受根据数据绑定条件,例如从一种风格更改< TextBlock />
的方法?我说风格,因为我知道你可以使用转换器像颜色,但如果我的变化变得相当复杂?例如添加边框,字体大小和背景颜色?
To create a scenario for a clear answer to this question, what is the best practice / most widely accepted way to change a <TextBlock />
for example from one style to another depending on a data binding condition? I say style, because I know you could use a Converter for something like a colour, but what if my changes become quite complex? For instance adding a border, font size and background colour too?
我的第二个场景是我要替换 Data
的< Path />
标签取决于视图模型条件,这也可能吗?基本上,我有一个'cross'和'tick'XAML路径,并希望根据视图模型属性将它们交换出来。
My second scenario is I want to replace the Data
of a <Path />
tag depending on a view model condition, is this also possible? Basically, I have a 'cross' and 'tick' XAML path and would like to swap them out depending on view model property.
我正在努力遵守MVVM所以也不喜欢在我的代码背后编写风格引用的硬编码。
I am trying to adhere to MVVM where possible as well, so would also prefer not to be hard-coding style references in my code behind.
感谢所有。
推荐答案
VisualStateManager
是你想要的。从此处:
管理
控件之间的状态和状态之间的逻辑。
Manages states and the logic for transitioning between states for controls.
我认为这是一般的,足以涵盖你想要的。
I think that is general enough to cover what you want.
来自同一链接的示例应该给你一些想法:
The example from the same link should give you some ideas:
<ControlTemplate TargetType="Button">
<Grid >
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<!--Take one half second to transition to the PointerOver state.-->
<VisualTransition To="PointerOver"
GeneratedDuration="0:0:0.5"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal" />
<!--Change the SolidColorBrush, ButtonBrush, to red when the
Pointer is over the button.-->
<VisualState x:Name="PointerOver">
<Storyboard>
<ColorAnimation Storyboard.TargetName="ButtonBrush"
Storyboard.TargetProperty="Color" To="Red" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid.Background>
<SolidColorBrush x:Name="ButtonBrush" Color="Green"/>
</Grid.Background>
</Grid>
</ControlTemplate>
请注意,您还可以更改 VisualStateManager
从代码。看看这个例子的默认模板中的LayoutAwarePage.cs。
It's important to note that you can also change the state of a VisualStateManager
from code. Look at the LayoutAwarePage.cs in the default templates for an example of this.
这篇关于Windows Store应用程序上的条件样式(或Style.Triggers-> DataTrigger等效)的最佳做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!