从两个不同的视图绑定到数据源。 MVVM、WPF

Binding to a data source from two different views. MVVM, WPF

这是设置。我有两个不同的 WPF 视图。标准工程视图和标准工程打印视图。我的 StandardEngineeredViewModel 中有一个名为 ModelRevisionList 的 ObservableCollection。

我正在做的是尝试从两个不同的视图绑定到 ModelRevisionList 数据源。我将我的视图的数据上下文设置为相同的视图模型。我的 StandardEngineeredView 中有一个按钮,可将我导航到 StandardEngineeredPrintView。第二个视图基本上只用于允许某人打印数据。请注意,我有几个其他属性将在两个视图的视图模型中绑定。我还没有尝试这样做,因为在尝试绑定到修订列表时出现以下错误:

System.Windows.Markup.XamlParseException HResult=0x80131501 Message='Add value to collection of type 'System.Windows.Controls.ItemCollection' threw an exception.' Line number '204' and line position '55'. Source=PresentationFramework StackTrace: at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri) at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri) at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream) at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator) at LabelPrinting.Views.EngineeringViews.StandardEngineeredPrintView.InitializeComponent() in C:\Users\eric_obermuller\source\repos\LabelPrinting\LabelPrinting\Views\EngineeringViews\StandardEngineeredPrintView.xaml:line 1

Inner Exception 1: InvalidOperationException: Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.

相关信息

StandardEngineeredViewModel 修订列表

private ObservableCollection<Revision> _ModelRevisionList = new ObservableCollection<Revision>();
public ObservableCollection<Revision> ModelRevisionList
{
    get { return _ModelRevisionList; } //Error thrown here upon get
    set { Set(ref _ModelRevisionList, value); }
}

绑定的StandarEngineeredView列表框

<ListBox
   BorderBrush="{StaticResource BackgroundBlueBrush}" 
   ItemsSource="{Binding ModelRevisionList}" 
   SelectedItem="{Binding SelectedRevision}"
   DisplayMemberPath="Date" 
   Margin="70 10" 
   MinHeight="150"
   MaxHeight="150"
   ItemStringFormat="{}{0:MM/dd/yyyy}"/>

绑定的StandardEngineeredPrintView gridview

<Border Grid.Column="1" MaxHeight="250" MaxWidth="400" MinWidth="400">
    <DataGrid ItemsSource="{Binding ModelRevisionList}" AutoGenerateColumns="False">
        <DataGridTextColumn Header="Date" Binding="{Binding Date}"/>
        <DataGridTextColumn Header="Edited By" Binding="{Binding EditedBy}"/>
        <DataGridTextColumn Header="Description" Binding="{Binding Description}"/>
    </DataGrid>
</Border>

在两个视图中设置的数据上下文

DataContext="{Binding StandardEngineeredViewInstance, Source={StaticResource Locator}}"

查看模型定位器

 public class ViewModelLocator
{
    /// <summary>
    /// Initializes a new instance of the ViewModelLocator class.
    /// </summary>
    public ViewModelLocator()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
        SetupNavigation();

        if (ViewModelBase.IsInDesignModeStatic)
        {
            // Create design time view services and models
            SimpleIoc.Default.Register<IDataAccessService, DataAccessService>();
        }
        else
        {
            // Create run time view services and models

            SimpleIoc.Default.Register<IDataAccessService, DataAccessService>();
        }
    }

    public StandardEngineeredViewModel StandardEngineeredViewInstance
    {
        get
        {
            SimpleIoc.Default.Register<StandardEngineeredViewModel>();
            return ServiceLocator.Current.GetInstance<StandardEngineeredViewModel>();
        }
    }

    public static void Cleanup()
    {
        // TODO Clear the ViewModels
    }
    private static void SetupNavigation()
    {
        SimpleIoc.Default.Unregister<IFrameNavigationService>();
        var navigationService = new FrameNavigationService();
        navigationService.Configure("StandardEngineeredPrintView", new Uri("/Views/EngineeringViews/StandardEngineeredPrintView.xaml", UriKind.Relative));
        SimpleIoc.Default.Register<IFrameNavigationService>(() => navigationService);
    }
}

现在由于错误,我认为这与我在导航到 StandardEngineeredPrintView 之前在我的 StandardEngineeredView 中使用可观察集合这一事实有关。您能否将两个视图绑定到同一个数据源?我做错了什么我只是不知道吗?

我很感激任何建议,如果你想要更多 information/code 就这么说吧。

谢谢!

好的,解决方案非常简单。我的数据网格设置不正确。我不得不将其更改为:

<DataGrid ItemsSource="{Binding ModelRevisionList}" AutoGenerateColumns="False">
     <DataGrid.Columns>
          <DataGridTextColumn Header="Date" Binding="{Binding Date}"/>
          <DataGridTextColumn Header="Edited By" Binding="{Binding EditedBy}"/>
          <DataGridTextColumn Header="Description" Binding="{Binding Description}"/>
     </DataGrid.Columns>
</DataGrid>

忘了DataGrid.Columns