8
我在写一个使用DataTrigger在ViewModel中设置属性的XAML文件。该视图模型类定义为:如何使用DataTrigger设置WPF中的ViewModel中定义的属性
public class ShellModel : INotifyPropertyChanged
{
public Brush ForegroundBrush
{
get; set;
}
....................
}
我想使用DataTrigger在View.xaml设置属性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" />
到
<Setter Property="ShellModel.ForegroundBrush" Value="Black" />
它给我的错误:
Dependency property field missing ....
我怎么写这篇文章,以使DataTrigger可以设置视图模型的财产ForegroundBrush?