【问题标题】:WPF: ItemSource not showing any itemWPF:ItemSsource 未显示任何项目
【发布时间】:2020-04-07 21:51:57
【问题描述】:

WPF 的代码非常简单:

<telerik:RadGridView Name="AnalisiKey" 
                              AutoGenerateColumns="True"
                                         Margin="10,273,694,59" 
                              d:DataContext="{d:DesignInstance Type=viewModels:FrequentKeywordFinderViewModel, IsDesignTimeCreatable=True}"
                              ItemsSource="{Binding ItemCollectionViewSourceSingole}" 
                              ClipboardCopyMode="All" SelectionMode="Extended" SelectionUnit="Mixed">
      <!--<telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn  x:Name="Keyword" Header="Keyword" Language="it-it" DataMemberBinding="{Binding (viewModels:KeyFreq.Keyword)}" />
        <telerik:GridViewDataColumn x:Name="FreqNelDocum" Header="FreqNelDocum" Language="it-it" UniqueName="FreqNelDocum"/>
      </telerik:RadGridView.Columns>-->
    </telerik:RadGridView>

以及 ViewModel

class FrequentKeywordFinderViewModel : MarkupExtension
  {
    public override object ProvideValue(IServiceProvider serviceProvider) => this;

    public List<KeyFreq> ItemCollectionViewSourceSingole { get; set; } = new List<KeyFreq>();
  }

以及填充 ItemSource 的代码:

 private void MostroRisultatiSuGriglia(List<KeyFreq> singole,
          List<KeyFreq> doppie, bool excludeUnfrequentKeys)
        {
          var dataContext = ((FrequentKeywordFinderViewModel)this.DataContext);
          var itemCollectionViewSourceSingole = dataContext.ItemCollectionViewSourceSingole;

          singole = CalcolaTfIdf(StopWordsUtil.FrequenzaKeywords, singole);

            dataContext.ItemCollectionViewSourceSingole.AddRange(singole.Where(s => s.FreqNelDocum > 1).ToList());
            itemCollectionViewSourceDoppie.Source = doppie.Where(s => s.FreqNelDocum > 1).ToList();              

        }

使用 Snoop,我可以深入研究 datagrid.ItemSource 并查看项目。但它们不会出现在数据网格中。有什么建议吗?

【问题讨论】:

  • 不清楚 MarkupExtension 是从什么派生的,但是 (1) 您的 ViewModel 类应该从 INotifyPropertyChanged 派生,并且 (2) 您的 ItemCollectionViewSourceSingole 设置器应该调用 PropertyChanged 事件并且 (3) 您的 ItemCollectionViewSourceSingole 应该是 ObservableCollection 而不是而不是一个列表,因为你在一个不同于它的构造的动作中填充它。
  • @Zenilogix:非常感谢。您可以将其转换为答案吗?然后我会添加一些额外的代码或截图来完成它。

标签: c# .net wpf datagrid


【解决方案1】:

使用绑定时要注意的一个关键点是控件不会从绑定的属性中更新,除非并且直到它被通知值已更改。实现此通知有两种基本方法:

  1. INotifyPropertyChanged 继承您的 ViewModel,并在您的属性值更改时调用 PropertyChanged 事件。这种方法适用于大多数情况,包括绑定到TextBlockTextBox 等控件的数字和字符串属性。

  2. ObservableCollection 用于绑定到ItemsSource 属性的集合(用于具有ItemsSource 属性的控件)。

控件知道INotifyPropertyChanged 接口和ObservableCollection 底层的INotifyCollectionChanged 接口,并侦听适当的PropertyChangedCollectionChanged 事件。

选择适当技术的指南如下:

  • 如果 ViewModel 中的属性值在控件的 DataContext 设置为 ViewModel 之前设置,并且随后从未更改,则实际上根本不需要使用 PropertyChanged 通知,因为控件将看到绑定 ViewModel 时的预期属性值。
  • 如果您绑定到一个属性,该属性的值将被初始分配或将更改DataContext 已设置为 ViewModel,ViewModel 必须继承自 INotifyPropertyChanged 和属性设置器必须调用PropertyChanged 事件,否则控件将永远不会知道属性值已更改。
  • 如果要将集合绑定到控件的 ItemsSource 属性,则需要考虑上述情况,但还需要考虑填充或更新集合内容的方式和时间。
  • 如果您正在创建和填充一个集合(如列表),则设置 ViewModel 的属性(绑定到控件的 ItemsSource 属性)并且永远不要修改集合的内容(尽管您可以稍后将 ViewModel 属性分配给不同的集合),ViewModel 必须从INotifyPropertyChanged 继承,并且集合属性设置器必须调用PropertyChanged 事件。在这种情况下,您实际上不需要考虑 ObservableCollection;您可以在 ViewModel 中使用任何所需的集合类型。
  • 如果在绑定到控件的ItemsSource 属性时修改集合的内容,则需要CollectionChanged 事件才能正确更新控件;最简单的方法是在 ViewModel 中使用 ObservableCollection;它会在添加或删除项目时自动引发 CollectionChanged 事件。

这些是有助于识别和解决使用绑定时最常见/最可能出现的问题的基本准则。

典型的属性绑定:

public class MyViewModel : INotifyPropertyChanged
{
    private string _myString;
    public string MyString
    {
        get => _myString;
        set
        {
            _myString = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(MyString));
        }
    }
}

在您的情况下,您可能只需将 ItemCollectionViewSourceSingoleList&lt;KeyFreq&gt; 更改为 ObservableCollection&lt;KeyFreq&gt;,因为您在 ViewModel 构造函数中初始化了空集合,并且稍后才添加项目。

【讨论】:

    猜你喜欢
    • 2020-08-09
    • 1970-01-01
    • 2016-06-29
    • 2015-07-01
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 2017-06-13
    • 1970-01-01
    相关资源
    最近更新 更多