I have created the following view in 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>
The issues I have is that when I am adding a new answer, the properties to which this view is bound are by default invalid. How do I get the validation to indicate this. In other words, how do I force the validation rules to apply to the properties when the form is first loaded? If its not obvious I am using MVVM. Here is my view model. It's pretty straight forward.
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.
}
}