[编辑] *我忘了包括异常所返回如何改变WPF矩形样式以编程方式(MVVM)(菜鸟)
'System.Windows.Style' is not a valid value for property 'Fill'
*
这是我的第一篇文章,我已经做了大量的搜索问题,这可能已经被回答为不成功。对于MVVM,Prism和WPF,我是一个完整的noob,而我在这一个领域已经陷入了深层次的问题。
我希望根据模型(绑定已通过)中的布尔值更改2个矩形的内容。
如果测试返回Pass(bool True),那么我们显示第一个矩形的颜色为绿色,第二个矩形将显示UserControl.Resource样式x:Key =“RectanglePass”。 如果测试失败,则第一矩形是红色的,显示器和第二显示器UserControl.Resource风格X:键=“RectangleFail”
我有以下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>
上面不起作用,并且返回一个异常。 如果我要用这一行代替第二个Rectangle标签(rectStatusImg)
<Rectangle Name="rectStatusImg" Style="{StaticResource RectanglePass}" Width="120" Height="120" Grid.Row="2" ></Rectangle>
xaml工作并产生预期的结果!但我希望将它绑定到布尔传递,所以我可以通过编程来改变它。
我真的很希望我能在这里解释我的问题。
非常感谢您提前给予的帮助,本网站一直以来都是宝贵的资助和帮助。
干杯, 西蒙
你是否在任何地方设置UserControl的DataContext?看起来事情的设置是正确的,包括绑定,但是不清楚你是否真的设置了DataContext来显示系统的值实际来自哪里。 – Tim
Hi @Tim,在fileView.xaml.cs中它有一个fileViewModel属性,它返回DataContext作为fileViewMode。有趣的是通过向网格添加DataContext。它停止产生一个异常。 – sayo9394