【问题标题】:Collectionviewsource Datagrid WPF MVVM Not showing rows [duplicate]Collectionviewsource Datagrid WPF MVVM不显示行[重复]
【发布时间】: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

请注意,我有一个继承自 INotifyPropertyChangedBaseViewModel

    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


【解决方案1】:

谢谢,我找到了答案。我没有将 Batch 类的成员定义为 properties。因此,尽管我们将值设置为公共成员,但它们不会出现在 .XAML 中,因为它们没有作为属性公开。如下更改它们对我有用。

public class Batch
{
    public DateTime p_dCreated { get; set; }
    public int p_iReference { get; set; }
    public BatchStatus p_iBatchStatus { get; set; }
    public string p_sFromBranch { get; set; }
}

【讨论】:

    猜你喜欢
    • 2012-09-17
    • 1970-01-01
    • 2016-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    相关资源
    最近更新 更多