我正在MVVM WPF中工作。我在弹出面板中包含一个listBox。显示弹出窗口,但listBox未填充,没有任何想法。list box用于过滤一列数据。这是列表框:
<ListBox x:Name="listBoxPopupContent" ItemsSource="{Binding ClassViewMethod}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsChecked}" Content="{Binding ClassName}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
和模型 View :
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");
}
DataContext设置为(在app.xaml中):
<Application.Resources>
<local:MainWindowViewModel x:Key="MainWindowViewModel" />
</Application.Resources>
并在MainWindow.xaml中:
DataContext="{StaticResource MainWindowViewModel}"
最佳答案
您需要验证页面的DataContext是否已设置为VM的 Activity 实例。
其次,VM需要实现INotifyPropertyChanged
来发出信号,通知存储在ClassViewMethod
的项目具有更改的项目。
将目标设为ObservableCollection
不会影响该动态。因此,当页面应查看集合时,会发出调用通知事件的信号,并且控件会加载数据。我在您的示例中没有看到该通知事件,因此没有两个可能的故障点之一。
我在我的博客文章Xaml: ViewModel Main Page Instantiation and Loading Strategy for Easier Binding上展示了所有这些内容
关于c# - 未在MVVM中填充组合框列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46974780/