我正在编写一个使用DataTrigger设置ViewModel中属性的XAML文件。ViewModel类定义如下:
public class ShellModel : INotifyPropertyChanged
{
public Brush ForegroundBrush
{
get; set;
}
....................
}
我希望在View.xaml中使用DataTrigger来设置ForegroundBrush属性。我编写的XAML如下:
<StatusBar Name="statusBar" Grid.Row="3">
<StatusBarItem>
<StatusBarItem.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding HasError}" Value="True">
<Setter Property="ForegroundBrush" Value="Red" />
</DataTrigger>
<DataTrigger Binding="{Binding HasError}" Value="False">
<Setter Property="ForegroundBrush" Value="Black" />
</DataTrigger>
</Style.Triggers>
</Style>
</StatusBarItem.Style>
<TextBlock Name="statusBarMessage" Foreground="{Binding ForegroundBrush}" Text="{Binding StatusMessage}"></TextBlock>
</StatusBarItem>
........................
这段代码无法编译。当我更改了 的位置后,代码可以编译。
<Setter Property="ForegroundBrush" Value="Black" />
to
<Setter Property="ShellModel.ForegroundBrush" Value="Black" />
我遇到了错误:
依赖属性字段缺失....
我该如何编写代码,以便DataTrigger可以在ViewModel中设置ForegroundBrush属性?