0
ValidationRule가 호출되지 않는WPF 내가 TextBlock의의이 XAML을 가지고
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);
}
}
:
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));
}
}
및 유효성 검사 규칙은 이것이다 FilesPath 속성이 다른 이벤트에 의해 업데이트되고 있습니다. (vm은 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;
}
}
파일 대화 상자에서 파일을 선택할 때 왜 ValidationRule이 호출되지 않습니까? 재산 (당신의 vm.FilesPath
속성) 소스로의 바인딩 대상 속성 (귀하의 경우 TextBlock.Text
)에서 데이터를 전송할 때
VS2010은 INotifyDataErrorInfo를 지원하지 않지만 IDataErrorInfo 만 지원합니까? –
@YonatanNir - INotifyDataErrorInfo는 .NET 4.5에서만 도입되었으며 VS2010은 .NET 4.0까지의 프레임 워크 버전 타겟팅 만 지원하므로 사용할 수 없습니다. – Grx70
하지만 이해가 안되는 이유는 어쨌든 작동하지 않는 이유입니다. INotifyDataErrorInfo는 바인딩을 통해 직접 속성을 변경할 때 사용하도록되어 있지만 INotifyPropertyChanged를 구현한다는 것을 이해합니다. 이는 validationRule과 함께 작동한다고 가정하지 않습니까? INotifyDataErrorInfo를 사용하면 규칙이 전혀 사용되지 않습니다. –