[英]Problems binding a DataGrid with an Observable Collection that receive null parameters in WPF
我是在WPF中使用C#进行的新编程,但出现错误:
我已经将WPF DataGRid与一个Observable集合绑定,该集合从数据库接收参数。 首先,当Observable Collection从存储过程中接收所有参数时,APP函数通常正常运行,但是当我在数据源中存储了null参数(StoredProcedure)时,Observable Collection接收到null参数,则应用程序崩溃,并且它没有开始...。页面只是保持空白
如何解决这个问题?
首先在这里,我向页面展示用作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 + "+++++++++++");
}
}
对于我的数据网格来说:
<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>
所以它是我想要的功能
对应于数据库中的表
但是,当我直接在数据库中输入NULL值时
该应用程序已启动,但每次仍为空白,因此出了点问题。
知道是什么问题吗?
像这样过滤:
if(MyProperty == null)
{
MyProperty = string.Empty;
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.