相关问题:WPF DataGrid Validation Bug?
您有以下DataGrid
<DataGrid ItemsSource="{Binding .}" AutoGenerateColumns="False" x:Name="dg">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name, ValidatesOnDataErrors=True}" Header="Name" />
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Command="{Binding ChangeNameCommand}">...</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
这似乎是一个错误。但是我可以做些什么来改变底层的ViewModel(例如,如果我使用的是OpenFileDialog)
视图模型:
public class ItemViewModel : INotifyPropertyChanged, IDataErrorInfo
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged("Name");
}
}
public string Error
{
get { return string.Empty; }
}
public string this[string columnName]
{
get
{
if (_name == "Error")
return "Name is Error";
return string.Empty;
}
}
public ICommand ChangeNameCommand { get; protected set; }
public ItemViewModel()
{
ChangeNameCommand = new RelayCommand(() => { Name = "Another Name"; });
}
}
DataContext设置为ItemViewModel的ObservableCollection