C# MVVM中未填充组合框列表
我在MVVM WPF工作。我在弹出面板中包含一个列表框。弹出窗口显示,但列表框未填充,不知道原因。列表框用于筛选一列数据。以下是列表框:C# MVVM中未填充组合框列表,c#,wpf,mvvm,listbox,C#,Wpf,Mvvm,Listbox,我在MVVM WPF工作。我在弹出面板中包含一个列表框。弹出窗口显示,但列表框未填充,不知道原因。列表框用于筛选一列数据。以下是列表框: <ListBox x:Name="listBoxPopupContent" ItemsSource="{Binding ClassViewMethod}"> <ListBox.ItemTemplate> <DataTemplate> <CheckBox
<ListBox x:Name="listBoxPopupContent" ItemsSource="{Binding ClassViewMethod}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsChecked}" Content="{Binding ClassName}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
您需要验证页面的DataContext是否设置为VM的活动实例 其次,虚拟机需要实现
INotifyPropertyChanged
,以表明存储在ClassViewMethod
中的项具有已更改的项
使目标成为可观察的集合
不会影响该动态。因此,当页面应该查看集合时,将发出通知事件的调用,控件将加载数据。在您的示例中,我没有看到notify事件,因此在两个可能的故障点中只有一个
我在我的博客文章中演示了这一切我对这段代码有点困惑。MainWindowViewModel是主窗口的视图模型。因此,我希望在MainWindow的构造函数中(由XAML创建),通过将viewmodel分配给Windows的DataContext属性来创建并附加viewmodel。但在这里,您的MainWindowViewModel有自己的DataContext属性。您正在分配控件(lstClassName、grdData等),就好像它是MainWindowViewModel的属性一样。看起来您的MainWindow和MainWindowViewModel是同一个类。我们可以看到xaml的
弹出窗口部分吗?这根本不是MVVM。在视图模型中指定UI元素属性毫无意义。也就是说,您似乎没有设置或绑定listBoxPopupContent的ItemsSource anywhere.listBoxPopupContent.ItemsSource=viewSource.View<代码>列表框
将为空,因为您没有在任何位置分配它的项资源。至于列表框,谢谢你的回答。我已经实现了notify属性,并且已经设置了DataContext。还是不起作用。。名单已经填好了,仅此而已。我编辑了我的问题,也许你有个想法。谢谢你advance@NicaNotifyPropertyChanged(“ClassViewMethod”)
在哪里完成?根据您的示例,如果没有该事件,combobox将不会通过反射加载数据。
public ObservableCollection<ClassView> ClassViewMethod
{
get
{
foreach (string cust in ClassViewItems.Select(w => w.ClassName).Distinct().OrderBy(w => w))
{
classFilters.Add(new CheckedListItem<string> { Item = cust, IsChecked = true });
}
viewSource.Filter += viewSource_Filter;
viewSource.Source = ClassViewItems;
return ClassViewItems;
}
}
private void viewSource_Filter(object sender, FilterEventArgs e)
{
ClassView cust = (ClassView)e.Item;
int count = classFilters.Where(w => w.IsChecked).Count(w => w.Item == cust.ClassName);
if (count == 0)
{
e.Accepted = false;
return;
}
e.Accepted = true;
}
public ObservableCollection<ClassView> ClassViewItems
{
get
{
return _classView;
}
set
{
_classView = value;
NotifyPropertyChanged("ClassViewItems");
}
<Application.Resources>
<local:MainWindowViewModel x:Key="MainWindowViewModel" />
</Application.Resources>
DataContext="{StaticResource MainWindowViewModel}"