Ste*_*fan 4 .net wpf xaml binding resourcedictionary
ResourceDictionary我想使用带有 的样式来定义控件的一般外观和感觉DataTrigger。
是否可以DataTrigger在视图中指定绑定路径?如果没有,是否有一些巧妙的替代方案可以实现我的目标?
我的目标是重用图形定义(包括触发器),但每次使用触发器时将触发器链接到不同的数据源。
样式示例:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Rectangle" x:Key="LedBehavior">
<Setter Property="Fill" Value="LightGray"/>
<Style.Triggers>
<DataTrigger Binding="{Binding **DefineThisPathInTheView**}" Value="True">
<Setter Property="Fill" Value="DarkGreen"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
在视图中我希望使用这种风格有点像这样:
<Rectangle Width="50" Height="50"
Style="{StaticResource LedBehavior}"
DataTriggerBindingPath="**PropertyInViewModel**"/>
Run Code Online (Sandbox Code Playgroud)
谢谢你!
该解决方案看起来非常简洁并且不太困难。
我必须将DataContext(在本例中)my绑定Rectangle到我想要的属性。然后我必须将我的风格的触发器绑定到DataContext.
下面的工作示例。
样式示例:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Rectangle" x:Key="LedBehavior">
<Setter Property="Fill" Value="LightGray"/>
<Style.Triggers>
<DataTrigger Binding="{Binding}" Value="True">
<Setter Property="Fill" Value="DarkGreen"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
示例(部分)视图:
<Rectangle Width="50" Height="50"
Style="{StaticResource LedBehavior}"
DataContext="{Binding PropertyInViewModel}"/>
Run Code Online (Sandbox Code Playgroud)
我希望它能帮助别人!
| 归档时间: |
|
| 查看次数: |
1813 次 |
| 最近记录: |