Unexpected red border (validation error) on DataGrid when selecting blank row

前端 未结 5 872
旧时难觅i
旧时难觅i 2020-12-31 00:44

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

相关标签:
5条回答
  • 2020-12-31 01:24

    You can just add this line to your DataGrid:

    <DataGrid  Validation.ErrorTemplate="{x:Null}" />
    

    It will solve the problem

    0 讨论(0)
  • 2020-12-31 01:32

    Here's a general-purpose converter you can use for any DataGrid, binding any kind of item:

        public class DataGridItemConverter : MarkupExtension, IValueConverter
        {
        static DataGridItemConverter converter;
    
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
            return value;
            }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
            return (value != null && value.GetType() == targetType) ? value : null;
            }
    
        public override object ProvideValue(IServiceProvider serviceProvider)
            {
            if (converter == null)
                converter = new DataGridItemConverter();
            return converter;
            }
        }
    

    Since it implements MarkupExtension you don't even need to define a static resource, you can just reference it like this:

    SelectedItem="{Binding SelectedThing,Converter={conv:DataGridItemConverter}}"
    
    0 讨论(0)
  • 2020-12-31 01:34

    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.

    0 讨论(0)
  • 2020-12-31 01:34

    You can just add this line to your DataGrid:

    <DataGrid  Validation.ErrorTemplate="{x:Null}" />
    
    0 讨论(0)
  • 2020-12-31 01:36

    To get the reason, when you click the new row of DataGrid in Debug mode, please see the debug window. There are first exception messages which will give you the idea why your problem is occurred.

    Yes, the problem is from type casting. You need to modify the type of SelectedItem to object type as below.

    public class ManageModulesVM : BaseVM  // Implements INotifyPropertyChanged
    {
        // ...
    
        public object SelectedConfigFile
        {
            get { return selectedModule == null ? null : selectedModule.SelectedConfigFile; }
            set
            {
                if (value != null)
                {
                    selectedModule.SelectedConfigFile = value;
                }
                OnPropertyChanged(() => SelectedConfigFile);
                OnPropertyChanged(() => Parameters);
            }
        }
    
        // ...
    }
    
    0 讨论(0)
提交回复
热议问题