选择空白行时DataGrid出现意外的红色边框(验证错误) [英] Unexpected red border (validation error) on DataGrid when selecting blank row

查看:230
本文介绍了选择空白行时DataGrid出现意外的红色边框(验证错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我选择(通过单击或通过键盘)空格行在我的DataGrid(当我想添加新的行),意外的验证错误发生(但也不例外) - datagrid的边框变为红色,因为你可以看到下面的图像。当我第二次点击空白行时,红色边框消失。一切其他工作正常,新行被添加。此外,我没有任何验证规则。而当我用空文本排列时,值是有效的。

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:

xaml中的DataGrid定义:

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>

我的ViewModel的一部分:

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类:

ConfigFile class:

public class ConfigFile
{
    public string Name { get; set; }
    public IList<Parameter> Parameters { get; set; }

    public ConfigFile() { Name = ""; Parameters = new List<Parameter>(); }
}

编辑:
经过进一步的调查,我知道,SelectedItem绑定导致问题(当我删除此绑定,验证错误停止显示),但我仍然不知道为什么以及如何解决这个问题。

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 }})

转换器类:

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;
        }
    }
}

定义 resources.xaml 文件(或任何其他资源位置):

Define resource in resources.xaml file (or in any other resources place):

<ResourceDictionary (...) xmlns:conv="clr-namespace:Converters" >
    <conv:SelectedConfigFileConverter x:Key="configFileConverter" />
</ResourceDictionary>

此解决方案的优点是 SelectedConfigFile 属性的类型没有更改(对于一般的对象类型),所以它仍然是强类型。

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.

这篇关于选择空白行时DataGrid出现意外的红色边框(验证错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆