When I select (by clicking or by keyboard) blank row on my DataGrid (when I want to add new row), unexpected validation error occurs (but with no exception) - the border of datagrid changes to red color, as you can see on the image below. When I click second time on blank row, the red border dissapears. Everything other works fine, the new row is added. Besides, I don't have any validation rules. And when I make a row with empty text, value is valid.
I don't want this behavior and this red border, anybody knows, why this happens and how to fix it? Why and where some validation fails?
Below I append some source code:
DataGrid definition in xaml:
<DataGrid IsSynchronizedWithCurrentItem="True" DisplayMemberPath="Name" ItemsSource="{Binding Path=ConfigFiles}" SelectedItem="{Binding Path=SelectedConfigFile}" Grid.Column="1" Height="87" Margin="0,26,11,32" Style="{DynamicResource DataGridStyle}"> <DataGrid.Columns> <DataGridTextColumn Width="1*" Binding="{Binding Name}" /> </DataGrid.Columns> </DataGrid>
My ViewModel's part:
public class ManageModulesVM : BaseVM // Implements INotifyPropertyChanged { // ... public ObservableCollection<ConfigFile> ConfigFiles { get { return selectedModule == null ? null : selectedModule.ConfigFiles; } set { selectedModule.ConfigFiles = value; OnPropertyChanged(() => ConfigFiles); } } public ConfigFile SelectedConfigFile { get { return selectedModule == null ? null : selectedModule.SelectedConfigFile; } set { if (value != null) { selectedModule.SelectedConfigFile = value; } OnPropertyChanged(() => SelectedConfigFile); OnPropertyChanged(() => Parameters); } } // ... }
ConfigFile class:
public class ConfigFile { public string Name { get; set; } public IList<Parameter> Parameters { get; set; } public ConfigFile() { Name = ""; Parameters = new List<Parameter>(); } }
Edit: After further investigation I know, that SelectedItem Binding is causing problems (when I remove this binding, validation error stops to appear), but I still don't know why and how to fix this.
I've found my own solution to this question. I've written a value converter and tied it to the binding:
(SelectedItem="{Binding Path=SelectedConfigFile,Converter={StaticResource configFileConverter}}")
The converter class:
namespace Converters { public class SelectedConfigFileConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return value; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { if(value is ConfigFile) return value; return null; } } }
Define resource in resources.xaml
file (or in any other resources place):
<ResourceDictionary (...) xmlns:conv="clr-namespace:Converters" > <conv:SelectedConfigFileConverter x:Key="configFileConverter" /> </ResourceDictionary>
The advantage of this solution is that the SelectedConfigFile
property's type did't changed (to the general object
type) so it is still strongly typed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With