【发布时间】:2011-11-16 10:47:34
【问题描述】:
我有一个带有 MVVM 的 WPF 应用程序,它有一个看起来像这样的 Ap.XAML。
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Assets/StyleDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
<ObjectDataProvider x:Key="SAG_POK" ObjectType="{x:Type or:SAG_POK}" />
</ResourceDictionary>
</Application.Resources>
现在,我想在 MainWindow.XAML 上绑定到 Ap.xaml 中的 SAG_POK ObjectDataprovider。
<StackPanel
DataContext="{Binding Source={StaticResource SAG_POK}}">
<TextBlock Name="ValgtSag" Text="{Binding ToStringProperty}"/>
</StackPanel>
我的问题是,在我的一个视图模型中,我在 App.xaml 中使用 SAG_POK 实例实例化了 SAG_POK ObjectDataProvider。
App.Current.Resources["SAG_POK"] = SagSelecteditem;
但我不知道将我的 OnNotifyPropertyChanged("SAG_POK") 放在哪里,我尝试了不同的方案,但似乎都没有工作。
任何人之前尝试过这个吗?请让我知道任何提示,提前谢谢。
【问题讨论】:
-
我不明白:当你已经拥有 ViewModel 时,为什么还要使用 ObjetcDataProvider?为什么不直接将 View 绑定到 ViewModel 上呢?
-
因为 Objectdataprovider 在 Application 中的多个地方使用,并且因为 viewmodel 是在页面上使用的,而不是在窗口中使用的。
-
你不会“设置”这样的资源。它在引用时被实例化,并且每次都重用该实例。您尝试使用 ODP 的方式很奇怪。为什么不将您的 SAG_POK 实例添加为资源?将 ODP 替换为以下内容:
<or:SAG_POK x:Key="SAG_POK" />