在创建新实体时如何验证加载表单?

时间:2014-12-18 20:59:12

标签: c# wpf validation mvvm

我在XAML中创建了以下视图:

<UserControl x:Class="CatalogEditor.NewAnswer"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:local="clr-namespace:CatalogEditor"
             mc:Ignorable="d"
             x:Name="UserControl"
             d:DesignWidth="640" d:DesignHeight="480">

    <Grid x:Name="LayoutRoot">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="1*"/>
        </Grid.RowDefinitions>

        <Label Content="Question:" Grid.Row="0" Margin="4" HorizontalAlignment="Right" VerticalAlignment="Center"/>
        <TextBlock Margin="4" Grid.Column="1" Grid.Row="0" Text="{Binding QuestionName, Mode=OneWay}"/>

        <Label Content="Name:" Grid.Row="1" Margin="4" HorizontalAlignment="Right" VerticalAlignment="Center"/>
        <TextBox Margin="4" Grid.Column="1" HorizontalAlignment="Left" MinWidth="300" MaxLength="50" Grid.Row="1" Padding="4,2">
            <TextBox.Text>
                <Binding Path="Name" NotifyOnValidationError="True">
                    <Binding.ValidationRules>
                        <local:RequiredRule />
                        <local:NoSpacesRule />
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>     
        </TextBox>

        <Label Content="Display:" Grid.Row="2" Margin="4" HorizontalAlignment="Right" VerticalAlignment="Center"/>
        <TextBox Margin="4" Grid.Column="1" HorizontalAlignment="Left" MinWidth="400" MaxLength="100" Grid.Row="2" Padding="4,2">
            <TextBox.Text>
                <Binding Path="EnglishDisplay" NotifyOnValidationError="True">
                </Binding>
            </TextBox.Text>     
        </TextBox>

        <Label Content="Display (Metric):" Grid.Row="3" Margin="4" HorizontalAlignment="Right" VerticalAlignment="Center"/>
        <TextBox Margin="4" Grid.Column="1" HorizontalAlignment="Left" MinWidth="400" MaxLength="100" Grid.Row="3" Padding="4,2">
            <TextBox.Text>
                <Binding Path="MetricDisplay" NotifyOnValidationError="True">
                </Binding>
            </TextBox.Text>     
        </TextBox>

        <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Row="5" Grid.Column="2" Orientation="Horizontal" Margin="2,0">
            <Button Content="Cancel" Width="75" Margin="2,4" Command="{Binding CancelCommand, Mode=OneWay}"/>
            <Button Content="Add" Width="75" Margin="2,4" FontWeight="Bold" Command="{Binding SaveCommand, Mode=OneWay}"/>
        </StackPanel>
    </Grid>
</UserControl>

我遇到的问题是,当我添加新答案时,此视图绑定的属性默认无效。如何获得验证以表明这一点。换句话说,如何在首次加载表单时强制验证规则应用于属性?如果不明显我正在使用MVVM。这是我的视图模型。它很直接。

public class AnswerViewModel
{
    private readonly Answer _answer;

    private string _name;
    private string _englishDisplay;
    private string _metricDisplay;

    public RelayCommand SaveCommand { get; private set; }
    public RelayCommand CancelCommand { get; private set; }

    public AnswerViewModel(Answer a_answer)
    {
        _answer = a_answer;

        Name = a_answer.Name;

        EnglishDisplay = a_answer.EnglishDisplay;

        MetricDisplay = a_answer.MetricDisplay;

        QuestionName = a_answer.Question.Name;

        SaveCommand = new RelayCommand(OnSaveCommandExecuted);
        CancelCommand = new RelayCommand(OnCancelCommandExecuted);
    }

    public AnswerViewModel()
    {
        _answer = new Answer();
    }

    public Answer Answer
    {
        get { return _answer; }
    }

    public String QuestionName { get; private set; }

    public String Name
    {
        get { return _name; }
        set { _name = value; RaisePropertyChanged(() => Name); }
    }

    public String EnglishDisplay
    {
        get { return _englishDisplay; }
        set { _englishDisplay = value; RaisePropertyChanged(() => EnglishDisplay); }
    }

    public String MetricDisplay
    {
        get { return _metricDisplay; }
        set { _metricDisplay = value; RaisePropertyChanged(() => MetricDisplay); }
    }

    private void OnSaveCommandExecuted()
    {
       _answer.Name = Name;
       _answer.EnglishDisplay= EnglishDisplay;
       _answer.MetricDisplay= MetricDisplay;

       // Navigate away.
    }

    private void OnCancelCommandExecuted()
    {
       // Navigate away.
    }

}

2 个答案:

答案 0 :(得分:0)

我将在这里尝试一个答案,但我并不是100%确定我会按照你的问题。

如果要强制对模型进行验证,则必须手动触发绑定更新,可以通过以下方式执行此操作:

somControl.GetBindingExpression(TextBox.TextProperty).UpdateSource();

(来自:) Force validation on bound controls in WPF

您可以在UserControl的Loaded事件的上传中执行此操作。

这是你在找什么?

答案 1 :(得分:0)

您必须在ValidationRule xaml上添加 ValidatesOnTargetUpdated =&#34; True&#34; ,以强制规则在目标对象更新时运行。然后,当您第一次加载datacontext时,您必须在需要验证的属性上触发属性更改事件,以重新计算验证规则。