[英]How to change WPF Rectangle Style Programatically (MVVM) (Noob)
[EDIT] *I had forgotten to include the Exception returned [编辑] *我忘了包含退回的异常
'System.Windows.Style' is not a valid value for property 'Fill'
* 'System.Windows.Style' is not a valid value for property 'Fill'
*
This is my first post, I've already done lots of searching for questions where this might have already been answered to no success. 这是我的第一篇文章,我已经做了很多寻找可能已经得到回答但没有成功的问题。 I'm a complete noob when it comes to MVVM, Prism, and WPF and I have been thrown in the deep end on this one.
当谈到MVVM,Prism和WPF时,我是一个完整的菜鸟,我已经被深深地抛在了这一点上。
I wish to change the contents of 2 rectangles depending on a boolean in the Model (Binding Passed). 我希望根据Model(Binding Passed)中的布尔值更改2个矩形的内容。
if a test returns Pass (bool True) then we display colour Green for first rectangle, and the second rectangle will display the UserControl.Resource Style x:Key="RectanglePass". 如果测试返回Pass(bool True),则我们为第一个矩形显示绿色,第二个矩形将显示UserControl.Resource Style x:Key =“RectanglePass”。 If test fails, then first rectangle is red, and second displays UserControl.Resource Style x:Key="RectangleFail"
如果测试失败,则第一个矩形为红色,第二个显示UserControl.Resource Style x:Key =“RectangleFail”
I have the following xaml code: 我有以下xaml代码:
<UserControl x:Class="Integration.Memjet.Aoqc.Views.ProcessResultView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<Style x:Key="RectangleFail" TargetType="Rectangle">
<Setter Property="Fill">
<Setter.Value>
<VisualBrush>
....
</VisualBrush>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="RectanglePass" TargetType="Rectangle">
<Setter Property="Fill">
<Setter.Value>
<VisualBrush>
....
</VisualBrush>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60"></RowDefinition>
<RowDefinition Height="60"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Label VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="20">PASS</Label>
<Rectangle Name="rectStatus" Grid.Row="1">
<Rectangle.Style>
<Style TargetType="{x:Type Rectangle}">
<Style.Triggers>
<DataTrigger Binding="{Binding Passed}" Value="True">
<Setter Property="Fill" Value="Green" />
</DataTrigger>
<DataTrigger Binding="{Binding Passed}" Value="False">
<Setter Property="Fill" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>
<Rectangle Name="rectStatusImg" Width="120" Height="120" Grid.Row="2" >
<Rectangle.Style>
<Style TargetType="{x:Type Rectangle}">
<Style.Triggers>
<DataTrigger Binding="{Binding Passed}" Value="True">
<Setter Property="Fill" Value="{StaticResource RectanglePass}" />
</DataTrigger>
<DataTrigger Binding="{Binding Passed}" Value="False">
<Setter Property="Fill" Value="{StaticResource RectangleFail}" />
</DataTrigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>
</Grid>
</UserControl>
The above doesn't work and returns an exception. 以上不起作用并返回异常。 If I was to replace the second Rectangle tag (rectStatusImg) with this line;
如果我要用这一行替换第二个Rectangle标记(rectStatusImg);
<Rectangle Name="rectStatusImg" Style="{StaticResource RectanglePass}" Width="120" Height="120" Grid.Row="2" ></Rectangle>
the xaml works and produces the expected result! xaml工作并产生预期的结果! But I wish to bind it to the Boolean Passed so I can change it programmatically.
但我希望将它绑定到布尔传递,所以我可以通过编程方式更改它。
I really hope I was able to explain my problem here. 我真的希望我能在这里解释我的问题。
Thank you for your help in advance, this site has always been a treasure and a great help. 提前感谢您的帮助,这个网站一直是宝藏和很大的帮助。
Cheers, Simon 干杯,西蒙
define two visual brushes in your resource and create a style of rectangle with data trigger like 在资源中定义两个可视画笔,并使用数据触发器创建一个矩形样式
<VisualBrush x:Key="FailBrush">Black</VisualBrush>
<VisualBrush x:Key="PassBrush">Red</VisualBrush>
<Style x:Key="ColorRectangleStyle" TargetType="Rectangle">
<Setter Property="Fill" Value="{StaticResource FailBrush}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Passed}" Value="False">
<Setter Property="Fill" Value="{StaticResource PassBrush}"/>
</DataTrigger>
</Style.Triggers>
</Style>
and use it in rectangle like 并在矩形中使用它
<Rectangle Name="rectStatusImg" Width="120" Height="120" Grid.Row="2" Style="{StaticResource ColorRectangleStyle}" />
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.