0

我有一个带有 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>

我的问题是,在我的一个视图模型中,我使用 SAG_POK 实例在 App.xaml 中实例化 SAG_POK ObjectDataProvider。

App.Current.Resources["SAG_POK"] = SagSelecteditem;

但我不知道将我的 OnNotifyPropertyChanged("SAG_POK") 放在哪里我尝试了不同的场景,但它们似乎都不起作用。

任何人以前尝试过这个?,请让我知道任何提示,提前谢谢。

4

1 回答 1

1

你可以这样做

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Assets/StyleDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
        <!--<ObjectDataProvider x:Key="SAG_POK" ObjectType="{x:Type or:SAG_POK}" />-->
        <local:MainViewModel x:Key="SAG_POK" />
    </ResourceDictionary>        
</Application.Resources>


<StackPanel DataContext="{Binding Source={StaticResource SAG_POK}}">
   <TextBlock Name="ValgtSag" Text="{Binding ToStringProperty}"/>
</StackPanel>

App.Current.Resources["SAG_POK"].SelectedItem = SagSelectedItem;

MainViewModel 是洞应用程序中的主视图模型:-)

希望这可以帮助

于 2011-11-16T13:50:57.067 回答