我有一个应用程序,我想要一个项目集合的单个实例,可以由多个视图模型访问,其中每个视图模型都与一个视图完全相关。为此,我将集合放入了应用程序资源中,视图模型在其构造函数中获得了对该引用的引用,该引用可作为公共属性使用。
我在视图中有一个ListBox,并将其DataContext设置为视图模型,然后将其ItemsSource设置为引用应用程序中集合的视图模型属性。然后,ListItems绑定到集合中项目的属性。
不幸的是,虽然填充了集合,但ListBox却没有。
这是App.xaml:
<Application x:Class="TestBindingToAppResource.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestBindingToAppResource"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
</ResourceDictionary.MergedDictionaries>
<!--Declaring this here so that a single instance of the people list is available to the entire application.-->
<local:People x:Key="MyPeople"/>
</ResourceDictionary>
</Application.Resources>
</Application>
单个项目及其集合:
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace TestBindingToAppResource
{
// Contains the data for a single person.
public class Person : INotifyPropertyChanged
{
private string theFirstName;
public string FirstName { get { return theFirstName; } set { if (value != theFirstName) { theFirstName = value; NotifyPropertyChanged(); } } }
private string theLastName;
public string LastName { get { return theLastName; } set { if (value != theLastName) { theLastName = value; NotifyPropertyChanged(); } } }
public event PropertyChangedEventHandler PropertyChanged;
public Person()
{
FirstName = "John";
LastName = "Doe";
}
public Person(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class People : ObservableCollection<Person>
{
public People()
{
}
}
}
主窗口仅包含一个包含该列表的UserControl。
<Window x:Class="TestBindingToAppResource.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestBindingToAppResource"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:PeopleList x:Name="ListOfPeople"/>
</Grid>
</Window>
这里是包含列表框的UserControl:
<UserControl x:Class="TestBindingToAppResource.PeopleList"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:TestBindingToAppResource"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.DataContext>
<local:PeopleViewModel/>
</UserControl.DataContext>
<Grid>
<!--... Other objects ...-->
<StackPanel Orientation="Vertical" Margin="5,5,5,5">
<TextBlock Text="Here's a list of people"/>
<ListBox Name="ListOfPeople" ItemsSource="{Binding People}" Margin="5,0,5,5">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Name="theFirstName" Text="{Binding FirstName}" Margin="5,5,5,5"/>
<TextBlock Name="theLastName" Text="{Binding LastName}" Margin="0,5,5,5"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<!--... Other objects ...-->
</StackPanel>
<!--... Other objects ...-->
</Grid>
</UserControl>
最后,这是将它们联系在一起的视图模型:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace TestBindingToAppResource
{
// A view model that allows the list in the view to access the people list in the model.
public class PeopleViewModel
{
public People thePeople { get; private set; }
public PeopleViewModel()
{
// A single instance of the people array is instantiated in the Application object as a resource.
// This allows any number of view models to access a single instance of the data model.
thePeople = (People)Application.Current.Resources["MyPeople"];
// The view model should load the people collection.
LoadThePeople(thePeople);
}
private void LoadThePeople (People thePeople)
{
thePeople.Add(new Person("Adam", "Baker"));
thePeople.Add(new Person("Cindy", "Douglas"));
thePeople.Add(new Person("Edward", "Fox"));
thePeople.Add(new Person("Gloria", "Herbst"));
}
}
}
结果如下:
所以,我的主要问题当然是为什么这不起作用?
但是,我相信许多人有一个相同的目标,即拥有整个应用程序中的多个视图模型可以访问的数据模型的单个实例。通常如何完成?
我正在使用:
答案 0 :(得分:0)
用户控件的dataContext是一个PeopleViewModel
实例
<UserControl.DataContext>
<local:PeopleViewModel/>
</UserControl.DataContext>
然后将ListBox绑定到People
属性
<ListBox Name="ListOfPeople" ItemsSource="{Binding People}" Margin="5,0,5,5">
...,但是PeopleViewModel没有People
属性,只有thePeople
。所以做到:
<ListBox Name="ListOfPeople" ItemsSource="{Binding thePeople}" Margin="5,0,5,5">
您应该已经在Visual Studio的“输出”窗口中看到有关缺少属性People
的绑定警告