이것은 내게로 이전 요청 질문에 대한 후속 질문 :이WPF - ValidationRule가 호출되지 않는
WPF - ValidationRule is not being called
나는 내가 INotifyDataErrorInfo
를 구현해야하고 그래서하지만 여전히 아무튼 않았다 들었다 일하지 마라. 뷰 모델에서
<TextBlock VerticalAlignment="Center">
<TextBlock.Text>
<Binding Path="Path" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<viewModel:StrRule/>
</Binding.ValidationRules>
</Binding>
</TextBlock.Text>
</TextBlock>
:
private string _path;
public string Path
{
set
{
_path = value;
OnPropertyChange("Path");
}
get { return _path; }
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChange(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private Dictionary<String, List<String>> errors = new Dictionary<string, List<string>>();
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
private void SetErrors(string propertyName, List<string> propertyErrors)
{
// Clear any errors that already exist for this property.
errors.Remove(propertyName);
// Add the list collection for the specified property.
errors.Add(propertyName, propertyErrors);
// Raise the error-notification event.
if (ErrorsChanged != null)
ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
}
private void ClearErrors(string propertyName)
{
// Remove the error list for this property.
errors.Remove(propertyName);
// Raise the error-notification event.
if (ErrorsChanged != null)
ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
}
public System.Collections.IEnumerable GetErrors(string propertyName)
{
if (string.IsNullOrEmpty(propertyName))
{
// Provide all the error collections.
return (errors.Values);
}
else
{
// Provice the error collection for the requested property
// (if it has errors).
if (errors.ContainsKey(propertyName))
{
return (errors[propertyName]);
}
else
{
return null;
}
}
}
public bool HasErrors
{
get
{
return errors.Count > 0;
}
}
그리고 유효성 검사 규칙 :
public class StrRule : 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");
}
return new ValidationResult(true, null);
}
}
가 나는 또한 다음의 FileDialog과를 여는 버튼을 가지고 여기에
은 XAML입니다 ViewModels의 Path 속성을 업데이트합니다.TextBlock이 업데이트되면 바인딩 자체가 작동하고 set 속성이 호출되지만 유효성 검사 규칙 자체는 호출되지 않습니다. 여기서 누락되거나 잘못 된 것은 무엇입니까? 주석 ValidationRule
에 명시된 바와 같이
여기서'INotifyDataErrorInfo'의 코드는 무엇입니까? 이전 답변에서 설명한 것처럼 유효성 검사 결과는 대상 바인딩에서 원본으로 업데이트되는 경우에만 호출됩니다. 그러나 작동하지 않을 소스 값을 직접 설정하는 경우. 소스를 업데이트하는 경우 수동으로 검증을 수행 할 수없는 이유는 무엇입니까? –
좋아요. 귀하의 의견을 보자 마자 문제가 생겼습니다 ... 사용자가 파일을 선택하고 텍스트의 Text 속성 대신 VM에서 속성을 업데이트 할 수있는 찾기 버튼이 있습니다. 블록 자체. 내가 그것을 바꿨을 때, 그것은 효과가 있었다. 나는이 정보로 질문을 편집 할 것이고 당신은 대답 할 수 있고 그것을 받아 들일 것이다. –