在XAML代码中绑定数据时,WPF DataGrid ItemsSource绑定未显示数据

时间:2018-10-01 12:27:56

标签: c# wpf

DataGrid在XAML视图代码中进行绑定时,即使在XAML中定义了窗口数据上下文,在同一个视图上的所有其他文本框和组合框数据绑定也可以正常工作时,所有这些属性均未粘贴在以下代码中

查看代码

<?php
 if(isset($_POST['color'])){

    $color = $_POST['color'];

        echo ("Your favorite color is: </br>" .$color);
}
else if (empty($_POST['description'])) {

    echo ("You did't select any color!");

}
else if (isset($_POST['description'])) {

    $description = $_POST['description'];

    echo ("Your favorite color is: </br>" .$description);

}

else{

        echo ("You didn't select any color!");

}
?>

视图与视图模型绑定,该视图模型包含DataGrid绑定对象,某些对象和其他对象 ViewModel

<Window x:Class="MegaSoft.Views.Windows.SaleInvoiceDetialWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MegaSoft.Views.Windows"
        mc:Ignorable="d"
         d:DesignHeight="1500" d:DesignWidth="1200"
        xmlns:uc="clr-namespace:MegaSoft.UserControls"
        xmlns:vm="clr-namespace:MegaSoft.ViewModel"
        WindowState="Maximized"
        Title="Sale Invoice">
      <Window.DataContext>
        <vm:SaleInvoiceDetialViewModel x:Name="_SaleInvoiceDetialViewModel"/>
    </Window.DataContext>
<DataGrid MinHeight="300" MaxHeight="300" MaxWidth="1300" ItemsSource="{Binding Path=DataGridCollection,Mode=TwoWay,NotifyOnTargetUpdated=True,NotifyOnSourceUpdated=True,ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged,ValidatesOnExceptions=True}" Name="SaleInvoiceDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" SelectionUnit="CellOrRowHeader" ColumnWidth="Auto" AutoGenerateColumns="False" CanUserAddRows="True" CanUserDeleteRows="True" CanUserReorderColumns="False" >
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Id" Visibility="Collapsed" Binding="{Binding Path=Id,Mode=OneWay ,ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged,ValidatesOnExceptions=True}" Width="Auto" CanUserResize="False" ></DataGridTextColumn>
                            <DataGridTextColumn IsReadOnly="True" MaxWidth="100" Header="Sr. No"  Binding="{Binding Path=SRNo,Mode=OneTime ,ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged,ValidatesOnExceptions=True}" CanUserResize="False"></DataGridTextColumn>


                        </DataGrid.Columns>
                    </DataGrid>
</Window>

Datagrid表明,当我尝试使用

 public class SaleInvoiceDetialViewModel : INotifyPropertyChanged, IDataErrorInfo
    {
     public ObservableCollection<SaleInvoiceDetialDataGridViewModel> DataGridCollection
        {
            get { return _DataGridCollection; }
            set
            {
                _DataGridCollection = value;
                OnPropertyChanged("DataGridCollection");
            }
        }
 #region PropertyChange
        //public event PropertyChangedEventHandler PropertyChanged;
        public event PropertyChangedEventHandler PropertyChanged;
        /// <summary>
        /// Implemantation of Property change interface
        /// </summary>
        /// <param name="property">Name of property</param>
        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
// other properties 

}

但不是必需的。应该可以在Xmal中正常绑定

_FrmSaleInvoiceDetialWindow.SaleInvoiceDataGrid.ItemsSource = DataGridCollection ;

SaleInvoiceDetialDataGridViewModel

ItemsSource="{Binding Path=DataGridCollection}"

2 个答案:

答案 0 :(得分:0)

由于尚未初始化集合,因此您无法在网格中看到任何内容。 由于您尝试绑定DataGrid的ItemsSource属性,因此需要将数据提供给集合以实际看到任何差异。 然后,我假设您知道,使用ObservableCollection时,您可以很容易地维护集合的更新,有关此票证的更多信息,请参见:What is the use of ObservableCollection in .net?

回到您的问题,请尝试以下代码:

    public SaleInvoiceDetialViewModel()
    {
        DataGridCollection = new ObservableCollection<SaleInvoiceDetialDataGridViewModel>
        {
            new SaleInvoiceDetialDataGridViewModel(),
            new SaleInvoiceDetialDataGridViewModel()
        };
    }

编辑: 初始化后在我这边看起来如何: enter image description here

答案 1 :(得分:0)

感谢每个伙伴的贡献。默认情况下,数据网格属性的初始化对我有用

   private ObservableCollection<SaleInvoiceDetialDataGridViewModel> _DataGridCollection = new ObservableCollection<SaleInvoiceDetialDataGridViewModel>();

        public ObservableCollection<SaleInvoiceDetialDataGridViewModel> DataGridCollection
        {
            get
            {
                return _DataGridCollection;
            }
            set
            {
                _DataGridCollection = value;
                OnPropertyChanged("DataGridCollection");
            }
        }