我用尽我的互联网搜索,似乎无法找到基于数据绑定条件的造型我的Windows Store应用XAML元素的最佳做法是什么?
<Style.Triggers><DataTrigger>...</DataTrigger></Style.Triggers>
似乎没有可用在Windows 8商店应用程序就像是在WPF和视觉状态管理器仅仅是预先设定如交互状态MouseOver
是不是? 我怎样才能大大取决于我的底层视图模型改变我的UI?
要创建一个明确的答案的情景对这个问题,什么是最好的实践/最广为接受的方式来改变一个<TextBlock />
取决于数据绑定状态从一个风格,例如到另一个? 我说的风格,因为我知道你可以使用一个转换器,用于像一种颜色,但如果我的变化变成什么很复杂? 例如添加边框,字体大小和背景颜色呢?
我的第二个场景是我想更换Data
一的<Path />
取决于视图模型条件标签,这是也有可能? 基本上,我有一个“交叉”和“剔” XAML路径和想换出来取决于视图模型属性。
我试图坚持MVVM在可能的情况一样,所以也会不喜欢在我的代码硬编码的背后样式引用。
感谢所有。
该VisualStateManager
是你想要的。 从这里 :
管理状态和逻辑状态之间转换为对照。
我认为这是一般足以覆盖你想要什么。
从相同的链接的例子应该给你一些想法:
<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对于这样的一个例子。
文章来源: Best practice for conditional styling (or Style.Triggers->DataTrigger equivalent) on Windows Store app?