Collectionviewsource数据网格WPF MVVM不显示行
我是WPF和MVVM的新手。我试图在我的项目中使用Datagrid和Collectionviewsource。我已经达到了如下的水平,但是我的Datagrid没有显示任何行。下面是代码部分。我想知道我在这里错过了什么。请注意,我只发布了解决此问题所必需的代码部分 ScanBatchWindow.xaml.csCollectionviewsource数据网格WPF MVVM不显示行,wpf,mvvm,data-binding,datagrid,collectionviewsource,Wpf,Mvvm,Data Binding,Datagrid,Collectionviewsource,我是WPF和MVVM的新手。我试图在我的项目中使用Datagrid和Collectionviewsource。我已经达到了如下的水平,但是我的Datagrid没有显示任何行。下面是代码部分。我想知道我在这里错过了什么。请注意,我只发布了解决此问题所必需的代码部分 ScanBatchWindow.xaml.cs public partial class ScanBatchWindow : Window { public ScanBatchWindow() { Ini
public partial class ScanBatchWindow : Window
{
public ScanBatchWindow()
{
InitializeComponent();
ScanBatchViewModel pMV = new ScanBatchViewModel();
this.DataContext = pMV;
}
}
ScanBatchWindow.xaml
<Window x:Class="BatchManPOC.ScanBatchWindow"
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:BatchManPOC"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:cmdBehavior="clr-namespace:BatchManPOC.CmdBehavior"
xmlns:video="clr-namespace:BatchManPOC.Video"
xmlns:Custom="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
mc:Ignorable="d"
Title="ScanBatchWindow"
Height="800"
Width="800">
<Window.Resources>
<CollectionViewSource Source="{Binding p_ListBatches}" x:Key="CVS"/>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Custom:DataGrid ItemsSource="{Binding Source={StaticResource CVS}}" Margin="8" Grid.Row="0" AutoGenerateColumns="True" IsReadOnly="True">
</Custom:DataGrid>
</Grid>
</Window>
ScanBatchViewModel.cs
public partial class ScanBatchWindow : Window
{
public ScanBatchWindow()
{
InitializeComponent();
ScanBatchViewModel pMV = new ScanBatchViewModel();
this.DataContext = pMV;
}
}
请注意,我有一个BaseViewModel,它继承自INotifyPropertyChanged
public class ScanBatchViewModel : BaseViewModel
{
public CollectionViewSource CVS { get; set; }
public ObservableCollection<Batch> p_ListBatches { get; set; }
public class Batch
{
public DateTime p_dCreated;
public int p_iReference;
public BatchStatus p_iBatchStatus;
public string p_sFromBranch;
}
/// <summary>
/// Initializes a new instance of the ScanBatchViewModel class.
/// </summary>
public ScanBatchViewModel()
{
LoadBatches();
CVS = new CollectionViewSource();
}
public void LoadBatches()
{
//Add sample objects
p_ListBatches.Add(new Batch() { p_dCreated = DateTime.Now, p_iBatchStatus = BatchStatus.DOC_STATUS_CREATED, p_iReference = 1, p_sFromBranch = "7010888" });
p_ListBatches.Add(new Batch() { p_dCreated = DateTime.Now, p_iBatchStatus = BatchStatus.DOC_STATUS_DELETED, p_iReference = 2, p_sFromBranch = "7010999" });
p_ListBatches.Add(new Batch() { p_dCreated = DateTime.Now, p_iBatchStatus = BatchStatus.DOC_STATUS_RECEIVED, p_iReference = 3, p_sFromBranch = "7010000" });
p_ListBatches.Add(new Batch() { p_dCreated = DateTime.Now, p_iBatchStatus = BatchStatus.DOC_STATUS_SENT, p_iReference = 4, p_sFromBranch = "7010777" });
}
}
公共类ScanBatchViewModel:BaseViewModel
{
public CollectionViewSource CVS{get;set;}
公共可观测集合p_listbacks{get;set;}
公共类批处理
{
已创建公共日期时间p_;
公共国际参考;
公共状态p_iBatchStatus;
来自分支的公共字符串p_;
}
///
///初始化ScanBatchViewModel类的新实例。
///
公共ScanBatchViewModel()
{
LoadBatches();
CVS=新的CollectionViewSource();
}
公共void LoadBatches()
{
//添加示例对象
p_listbacks.Add(new Batch(){p_dCreated=DateTime.Now,p_iBatchStatus=BatchStatus.DOC_STATUS_CREATED,p_irereference=1,p_sFromBranch=“7010888”});
p_listbacks.Add(new Batch(){p_dCreated=DateTime.Now,p_iBatchStatus=BatchStatus.DOC_STATUS_DELETED,p_irereference=2,p_sFromBranch=“7010999”});
p_listbacks.Add(new Batch(){p_dCreated=DateTime.Now,p_iBatchStatus=BatchStatus.DOC_STATUS_RECEIVED,p_irereference=3,p_sFromBranch=“7010000”});
p_listbacks.Add(new Batch(){p_dCreated=DateTime.Now,p_iBatchStatus=BatchStatus.DOC_STATUS_SENT,p_irereference=4,p_sFromBranch=“7010777”});
}
}
谢谢,我找到了答案。我没有将批处理类的成员定义为属性。因此,尽管我们将这些值设置为公共成员,但它们不会显示在.XAML中,因为它们不会作为属性公开。按以下方式更改它们对我很有效
请在输出窗口中查找错误。可能有一些约束issue@ASh谢谢,刚刚看到你的评论。是的,原因与属性绑定有关。我一直在寻找这个答案,但没有找到。谢谢