【发布时间】:2018-06-13 07:35:20
【问题描述】:
我是 WPF 和 MVVM 的新手。我正在尝试在我的项目中使用 Datagrid 和 Collectionviewsource。我已经达到了如下水平,但我的 Datagrid 没有显示任何行。下面是代码部分。我试图弄清楚我在这里缺少什么。请注意,我仅发布此问题所需的代码部分。
ScanBatchWindow.xaml.cs
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
请注意,我有一个继承自 INotifyPropertyChanged 的 BaseViewModel。
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" });
}
}
【问题讨论】:
-
请在输出窗口中查找错误。可能存在一些绑定问题
-
@ASh 谢谢,刚刚看到你的 cmets。是的,原因与属性绑定有关。我一直在寻找这个答案,但找不到。谢谢。
标签: wpf mvvm data-binding datagrid collectionviewsource