C# tab使用不同视图/视图模型进行控件验证
我使用MVVM设计模式创建了一个WPF应用程序。MainView有一个带有两个项的tabcontrol,每个项都有不同的视图/视图模型,如以下代码所示C# tab使用不同视图/视图模型进行控件验证,c#,wpf,mvvm,tabcontrol,idataerrorinfo,C#,Wpf,Mvvm,Tabcontrol,Idataerrorinfo,我使用MVVM设计模式创建了一个WPF应用程序。MainView有一个带有两个项的tabcontrol,每个项都有不同的视图/视图模型,如以下代码所示 <TabControl> <TabItem Header="Test 2" DataContext="{Binding CurrentTest2ViewModel, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> <
<TabControl>
<TabItem Header="Test 2" DataContext="{Binding CurrentTest2ViewModel, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<AdornerDecorator>
<local:Test2View></local:Test2View>
</AdornerDecorator>
</TabItem>
<TabItem Header="Test 1" DataContext="{Binding CurrentTest1ViewModel, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<AdornerDecorator>
<local:Test1View></local:Test1View>
</AdornerDecorator>
</TabItem>
</TabControl>
在两个ViewModels中的一个中,我使用IDataErrorInfo接口验证属性。相应的视图具有以下代码
<StackPanel Orientation="Vertical">
<CheckBox HorizontalAlignment="Left" Width="80" Content="IsRed" IsChecked="{Binding IsRed, ValidatesOnDataErrors=True, NotifyOnValidationError=True}" IsEnabled="{Binding IsEnabled}">
<Validation.ErrorTemplate>
<ControlTemplate>
<StackPanel Orientation="Horizontal">
<AdornedElementPlaceholder />
<Label Content="{Binding Path=ErrorContent}" ToolTip="{Binding Path=ErrorContent}" />
</StackPanel>
</ControlTemplate>
</Validation.ErrorTemplate>
</CheckBox>
<CheckBox HorizontalAlignment="Left" Width="80" Content="IsGreen" IsChecked="{Binding IsGreen, ValidatesOnDataErrors=True, NotifyOnValidationError=True}" IsEnabled="{Binding IsEnabled}">
<Validation.ErrorTemplate>
<ControlTemplate>
<StackPanel Orientation="Horizontal">
<AdornedElementPlaceholder />
<Label Content="{Binding ErrorContent}" ToolTip="{Binding ErrorContent}" />
</StackPanel>
</ControlTemplate>
</Validation.ErrorTemplate>
</CheckBox>
</StackPanel>
当第一次显示该项时,验证会发现某些属性的值不正确(我可以通过断点看到),但错误消息不会出现在屏幕上。要再现错误消息,我需要在正确值之前选择,然后在值无效时显示错误消息
我的问题是:
1) 如何从项目第一次显示在屏幕上时复制验证的错误消息?
2) 如果我对任何项目只使用主视图而不使用不同的视图,为什么没有问题
非常感谢您。ErrorContent的代码是什么?你实现了吗?我实现了IDataErrorInfo接口。ErrorContent来自它。IDataErrorInfo不是从INotifyPropertyChanged继承的,您需要INotifyPropertyChanged接口才能更新属性绑定。请阅读上面的链接了解详细信息。当然,我实现了INotifyPropertyChanged。您可以发布属性ErrorContent的getter setter代码吗?