Я работаю над новым приложением C # 4.0 / Prism 4, используя JulMar Mvvm-Helpers , чтобы помочь с реализацией MVVM. У меня проблема с моей логикой проверки. В прошлом я успешно использовал Prism 2.2 / Enterprise Library Validation Block. Но для этого проекта я пробую что-то новое.
Ниже указан мой код XAML.
<UserControl x:Class="DoSomeThing.Views.DoSomeThingView"
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"
xmlns:Converters="clr-namespace:JulMar.Windows.Converters;assembly=JulMar.Wpf.Helpers"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" >
<UserControl.Resources>
<Converters:ValidationErrorConverter x:Key="errorConverter"/>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors), Converter={StaticResource errorConverter}}"/>
<Setter Property="Background" Value="Red" />
<Setter Property="Foreground" Value="White" />
</Trigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<Grid Name="EditGrid" Margin="3">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Grid.Column="1" Grid.Row="0" Content="Name" Height="21" VerticalAlignment="Top" />
<Label Grid.Column="1" Grid.Row="1" Content="Address" />
<Label Grid.Column="1" Grid.Row="2" Content="Zip" />
<Label Grid.Column="1" Grid.Row="3" Content="Number Of Doors" />
<Label Grid.Column="1" Grid.Row="4" Content="Double Number" />
<TextBox Grid.Column="2" Grid.Row="0" Text="{Binding Path=Name, Mode=TwoWay, UpdateSourceTrigger=LostFocus, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"
HorizontalAlignment="Left" VerticalAlignment="Top" Width="200" />
<TextBox Grid.Column="2" Grid.Row="1" Text="{Binding Path=Address, Mode=TwoWay, UpdateSourceTrigger=LostFocus, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"
HorizontalAlignment="Left" VerticalAlignment="Top" Width="200"/>
<TextBox Grid.Column="2" Grid.Row="2" Text="{Binding Path=Zip, Mode=TwoWay, UpdateSourceTrigger=LostFocus, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" HorizontalAlignment="Left"
VerticalAlignment="Top" Width="200"/>
<TextBox Grid.Column="2" Grid.Row="3" Text="{Binding Path=NumberOfDoors, Mode=TwoWay, UpdateSourceTrigger=LostFocus, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"
HorizontalAlignment="Left" Height="21" Width="200"/>
<TextBox Grid.Column="2" Grid.Row="4" Text="{Binding Path=DoubleNumber, Mode=TwoWay,
UpdateSourceTrigger=LostFocus, NotifyOnValidationError=True, ValidatesOnDataErrors=True,
ValidatesOnExceptions=True}"
HorizontalAlignment="Left" VerticalAlignment="Top" Width="200"/>
<Button Content="Save" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="4" Height="23"
HorizontalAlignment="Left" Margin="26,41,0,0" Name="button1" VerticalAlignment="Top"
Width="75" Command="{Binding SaveCommand}"/>
</Grid>
Код для привязки вида к модели вида:
IRegion region = this._regionManager.Regions["MainRegion"];
var v = new DoSomeThingView();
var model = new SampleDataModel
{
Name = "hello",
NumberOfDoors = 5,
Zip = "12345",
DoubleNumber = 321.12,
Address = "no where's ville"
};
var vm = new SampleDataViewModel { DataModel = model };
v.EditGrid.DataContext = vm;
region.Add(v);
Вся логика редактирования работает так же, как и атрибуты проверки в модели представления. Моя проблема с функцией bool CanSaveCommand(object param)
, чтобы предотвратить сохранение, если есть какие-либо ошибки проверки.
Я не вижу, как определить наличие ошибок проверки. Предложения?