C# WPF:ItemSource未显示任何项目
WPF代码非常简单:C# WPF:ItemSource未显示任何项目,c#,.net,wpf,datagrid,C#,.net,Wpf,Datagrid,WPF代码非常简单: <telerik:RadGridView Name="AnalisiKey" AutoGenerateColumns="True" Margin="10,273,694,59" d:DataContext="{d:DesignInstance Type=viewMo
<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>();
}
class FrequentKeywordFinderViewModel:MarkupExtension
{
公共覆盖对象ProviderValue(IServiceProviderServiceProvider)=>此;
public List ItemCollectionViewSourceSingole{get;set;}=new List();
}
以及填充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();
}
private void MostroRisultatiSuGriglia(单列),
列出doppie、bool(不包括非频繁键)
{
var dataContext=((FrequentKeywordFinderViewModel)this.dataContext);
var itemcollectionviewsourcessingole=dataContext.itemcollectionviewsourcessingole;
singole=CalcolaTfIdf(StopWordsUtil.FrequenzaKeywords,singole);
dataContext.ItemCollectionViewSourceSingole.AddRange(singole.Where(s=>s.frequenneldocum>1.ToList());
itemCollectionViewSourceDoppie.Source=doppie.Where(s=>s.frequenneldocum>1.ToList();
}
使用Snoop,我可以深入研究datagrid.ItemSource并查看项目。但它们不会出现在数据网格中。有什么建议吗
使用绑定时需要注意的一个关键点是,控件不会从绑定属性中更新,除非通知它值已更改。实施此通知有两种基本方式:
INotifyPropertyChanged
继承ViewModel,并在属性值更改时调用PropertyChanged
事件。这种方法适用于大多数情况,包括绑定到控件(如TextBlock
和TextBox
)的数字和字符串属性itemsource
属性的集合(对于具有itemsource
属性的控件),使用observeCollection
INotifyPropertyChanged
接口和INotifyCollectionChanged
接口底层observedcollection
,并侦听相应的PropertyChanged
和CollectionChanged
事件
选择适当技术的指南如下:
- 如果ViewModel中的属性值是在控件的
设置为ViewModel之前设置的,并且以后从未更改,那么实际上根本不需要使用DataContext
通知,因为绑定ViewModel时控件将看到预期的属性值PropertyChanged
- 如果要绑定到一个属性,该属性的值将被初始分配,或者在将
设置为ViewModel后将发生更改,则ViewModel必须从DataContext
继承,并且属性设置器必须调用INotifyPropertyChanged
事件,否则,控件将永远不会知道属性值已更改PropertyChanged
- 如果您将集合绑定到控件的<代码> ItEsStase属性,则需要考虑上面的内容,但还需要考虑如何和何时填充或更新集合的内容。
- 如果要创建和填充集合(如列表),请设置ViewModel的属性(绑定到控件的
属性),并且从不修改集合的内容(尽管以后可能会将ViewModel属性指定给不同的集合),ViewModel必须继承自ItemsSource
,集合属性设置程序必须调用INotifyPropertyChanged
事件。在这种情况下,您实际上不需要考虑观察存储;可以在ViewModel中使用任何所需的集合类型PropertyChanged
- 如果在集合绑定到控件的
属性时修改集合的内容,则需要ItemsSource
事件才能正确更新控件;实现这一点的最简单方法是在ViewModel中使用CollectionChanged
;添加或删除项目时,它将自动引发ObservableCollection
事件CollectionChanged
public class MyViewModel : INotifyPropertyChanged
{
private string _myString;
public string MyString
{
get => _myString;
set
{
_myString = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(MyString));
}
}
}
在您的情况下,您可能只需要将
ItemCollectionViewSourceSingole
从List
更改为observedcollection
,因为您在ViewModel构造函数中初始化空集合,以后才添加项。不清楚MarkupExtension的来源,但是(1)ViewModel类应派生自INotifyPropertyChanged,并且(2)ItemCollectionViewSourceSingle setter应调用PropertyChanged事件,以及(3)ItemCollectionViewSourceSingle应为ObservableCollection而不是列表,因为你用一种不同于其结构的动作来填充它。@Zenilogix:非常感谢。你能把它转换成答案吗?然后我会添加一些额外的代码或截图来完成它。