在WPF中绑定DataGrid与接收null参数的Observable Collection的问题

本文关键字:参数 null Observable 问题 Collection WPF 绑定 DataGrid | 更新日期: 2023-09-27 18:02:14

我在WPF中使用c#进行新编程,并且我有一个错误:

我已经绑定了我的WPF DataGRid与一个可观察集合,从数据库接收参数。首先,当Observable Collection从存储过程中接收到APP正常运行的所有参数时,但是当我的数据源(StoredProcedure)中有一个空参数时,所以Observable Collection接收到一个空参数,应用程序崩溃,并且它不能启动....页面保持空白

如何解决这个问题?

首先,这里我展示了我用作Dada上下文的类到我的页面

class ColeccionDeDatos : INotifyPropertyChanged
{
    private RegistroBLL regBll = new RegistroBLL();
    private DivisionBLL divBll = new DivisionBLL();
    private BrigadaBLL briBll = new BrigadaBLL();
    private BatallonBLL batBll = new BatallonBLL();
    private TropasBLL tropBll = new TropasBLL();
    private CompañiaBLL compBLL = new CompañiaBLL();
    private EstudioBLL estBll = new EstudioBLL();
    private RegistroFullBLL regFullBll = new RegistroFullBLL();

    private ObservableCollection<RegistroFullBO> coleccionFullRegistro = new ObservableCollection<RegistroFullBO>();
    public ObservableCollection<RegistroFullBO> ColeccionFullRegistro
    {
        get { return coleccionFullRegistro; }
        set { coleccionFullRegistro = value; }
    }
    private ObservableCollection<RegistroBO> coleccionRegistro = new ObservableCollection<RegistroBO>();
    public ObservableCollection<RegistroBO> ColeccionRegistro
    {
        get { return coleccionRegistro; }
        set { coleccionRegistro = value; }
    }
    private ObservableCollection<DivisionBO> coleccionDivision = new ObservableCollection<DivisionBO>();
    public ObservableCollection<DivisionBO> ColeccionDivision
    {
        get { return coleccionDivision; }
        set { coleccionDivision = value; }
    }
    private ObservableCollection<BrigadaBO> coleccionBrigada = new ObservableCollection<BrigadaBO>();
    public ObservableCollection<BrigadaBO> ColeccionBrigada
    {
        get { return coleccionBrigada; }
        set { coleccionBrigada = value; }
    }
    private ObservableCollection<BatallonBO> coleccionBatallon = new ObservableCollection<BatallonBO>();
    public ObservableCollection<BatallonBO> ColeccionBatallon
    {
        get { return coleccionBatallon; }
        set { coleccionBatallon = value; }
    }
    private ObservableCollection<TropasBO> coleccionTropas = new ObservableCollection<TropasBO>();
    public ObservableCollection<TropasBO> ColeccionTropas
    {
        get { return coleccionTropas; }
        set { coleccionTropas = value; }
    }
    private ObservableCollection<CompañiaBO> coleccionCompañia = new ObservableCollection<CompañiaBO>();
    public ObservableCollection<CompañiaBO> ColeccionCompañia
    {
        get { return coleccionCompañia; }
        set { coleccionCompañia = value; }
    }
    private ObservableCollection<EstudioBO> coleccionEstudio = new ObservableCollection<EstudioBO>();
    public ObservableCollection<EstudioBO> ColeccionEstudio
    {
        get { return coleccionEstudio; }
        set { coleccionEstudio = value; }
    }
    public ColeccionDeDatos() 
    {
        ColeccionRegistro = regBll.ObtenerFilasRegistro();
        ColeccionDivision = divBll.ObtenerFilasDivision();
        ColeccionBrigada = briBll.ObtenerFilasBrigada();
        ColeccionBatallon = batBll.ObtenerFilasBatallon();
        ColeccionTropas = tropBll.ObtenerFilasTropas();
        ColeccionCompañia = compBLL.ObtenerFilasCompañia();
        ColeccionEstudio = estBll.ObtenerFilasEstudio();
        ColeccionFullRegistro = regFullBll.ObtenerFilasRegistro();
    }

然后在页面后面的代码中,为page元素指定该类的一个实例作为DataContext。

private void Page_Loaded(object sender, RoutedEventArgs e)
    {
        try
        {
            PagRegistroName.DataContext = colData;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message + "+++++++++++");
        }
    }

对于我的Data Grid这个:

<DataGrid Name="dgRegistro" Margin="5" SelectionChanged="dgRegistro_SelectionChanged"
                            ItemsSource="{Binding Path=ColeccionFullRegistro}" AutoGenerateColumns="False">

在每个单元格模板中我使用了一个组合框:

<DataGridTemplateColumn.CellEditingTemplate >
                        <DataTemplate>
                            <ComboBox x:Name="cmbDivision" Text="{Binding Path=Division, Mode=TwoWay}"     
                                      ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Page, AncestorLevel=1},
                                      Path=DataContext.ColeccionDivision}"  DisplayMemberPath="Nom_division" SelectionChanged="cmbDivision_SelectionChanged" SelectedValuePath="Nom_division">
                            </ComboBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>

所以它是函数a

https://onedrive.live.com/redir?resid=B371651233630F9A ! 153, authkey = ! ADzRW6OvvdMZiMI& v = 3, ithint = % 2照片c.png

对应于DataBase

中的表

但是,当我直接在DataBase

中放入NULL值时http://1drv.ms/RrN5qO

应用程序启动,但每次仍然空白,所以有问题…

你知道是什么问题吗?

在WPF中绑定DataGrid与接收null参数的Observable Collection的问题

像这样过滤:

if(MyProperty == null)
{ 
    MyProperty = string.Empty;
]