0

I got this Xaml of a TextBlock:

<TextBlock VerticalAlignment="Center">
       <TextBlock.Text>
             <Binding Path="FilesPath" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
                  <Binding.ValidationRules>
                        <viewModel:ExtensionRule></viewModel:ExtensionRule>
                   </Binding.ValidationRules>
              </Binding>
         </TextBlock.Text>
 </TextBlock>

In the ViewModel:

    private string _filesPath;
    public string FilesPath
    {
        set 
        { 
            _filesPath = value;
            OnPropertyChange("FilesPath");
        }
        get { return _filesPath; }
    }

    private void OnPropertyChange(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

and the validation rule is this:

public class ExtensionRule : ValidationRule
{
    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    {
        string filePath = String.Empty;
        filePath = (string)value;
        if (String.IsNullOrEmpty(filePath))
        {
            return new ValidationResult(false, "Must give a path");
        }

        if (!File.Exists(filePath))
        {
            return new ValidationResult(false, "File not found");
        }
        string ext = Path.GetExtension(filePath);
        if (!ext.ToLower().Contains("txt"))
        {
            return new ValidationResult(false, "given file does not end with the \".txt\" file extenstion");
        }
        return new ValidationResult(true, null);
    }
}

and the FilesPath property is being updated by another event: (vm is the viewModel var)

private void BrowseButton_Click(object sender, RoutedEventArgs e)
    {
        // Create OpenFileDialog 
        OpenFileDialog dlg = new OpenFileDialog();

        // Set filter for file extension and default file extension 
        dlg.DefaultExt = ".txt";
        dlg.Filter = "txt Files (*.txt)|*.txt";

        // Display OpenFileDialog by calling ShowDialog method 
        bool? result = dlg.ShowDialog();

        // Get the selected file name and display in a TextBox 
        if (result == true)
        {
            // Open document 
            string filename = dlg.FileName;
            vm.FilesPath = filename;
        }
    }

Why doesn't the ValidationRule being called when i choose a file by the file dialog?

CodeMonkey
  • 11,196
  • 30
  • 112
  • 203

1 Answers1

4

According to this MSDN Library article validation rules are only checked when transferring data from binding's target property (TextBlock.Text in your case) to source property (your vm.FilesPath property) - the aim here is to validate user input from, for example, a TextBox. In order to give validation feedback from source property to the control owning the target property (the TextBlock control) your view model should implement either IDataErrorInfo or INotifyDataErrorInfo.

Grx70
  • 10,041
  • 1
  • 40
  • 55
  • Is it possible that VS2010 does not support INotifyDataErrorInfo, but only IDataErrorInfo? – CodeMonkey Mar 29 '14 at 22:09
  • @YonatanNir - The `INotifyDataErrorInfo` was only introduced in .NET 4.5 and VS2010 only supports targeting framework versions up to .NET 4.0, so it won't be available. – Grx70 Mar 29 '14 at 22:27
  • but what I don't understand is why doesn't it works anyway. I understand that INotifyDataErrorInfo is supposed to be used when changing the property directly and not via binding, but still the property implements INotifyPropertyChanged.. Doesn't it mean it's suppose to work with the validationRule? Since with INotifyDataErrorInfo, the rule will not be used at all – CodeMonkey Apr 01 '14 at 21:58