所以这是设置。我有两个不同的WPF视图。 StandardEngineeredView和StandardEngineeredPrintView。我在StandardEngineeredViewModel中有一个名为ModelRevisionList的ObservableCollection。
我正在做的是尝试从两个不同的视图绑定到ModelRevisionList数据源。我将我的视图的数据上下文设置为相同的视图模型。我的StandardEngineeredView中有一个按钮,可以将我导航到StandardEngineeredPrintView。第二个视图基本上只用于允许某人打印数据。注意我在视图模型中将从两个视图中绑定几个其他属性。我还没有尝试这样做,因为当尝试绑定到修订列表时,我得到以下错误:
System.Windows.Markup.XamlParseException HResult = 0x80131501 Message ='为类型集合'System.Windows.Controls.ItemCollection'添加值引发异常。行号'204'和行位置'55'。 Source = PresentationFramework StackTrace:System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader,IXamlObjectWriterFactory writerFactory,Boolean skipJournaledProperties,Object rootObject,XamlObjectWriterSettings设置,Uri baseUri),位于System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader,Boolean skipJournaledProperties) ,对象rootObject,XamlAccessLevel accessLevel,Uri baseUri)在LabelPrinting的System.Windows.Application.LoadComponent(Object component,Uri resourceLocator)的System.Windows.Markup.XamlReader.LoadBaml(Stream stream,ParserContext parserContext,Object parent,Boolean closeStream) C:\ Users \ eric_obermuller \ source \ repos \ LabelPrinting \ LabelPrinting \ Views \ EngineeringViews \ StandardEngineeredPrintView.xaml:line 1中的.Views.EngineeringViews.StandardEngineeredPrintView.InitializeComponent()
内部异常1:InvalidOperationException:当ItemsSource正在使用时,操作无效。使用ItemsControl.ItemsSource访问和修改元素。
相关信息
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);
}
}
现在由于这个错误,我认为这与我在使用StandardEngineeredView中的observable集合导航到StandardEngineeredPrintView之前的事实有关。您是否能够将两个视图绑定到同一数据源?我做错了什么我不知道吗?
我感谢任何建议,如果您想要更多信息/代码,请说出来。
谢谢!
好的,解决方案非常简单。我的数据网格设置不正确。我不得不改变它:
<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