2011-11-16 46 views
0

我有一個MVVM應用程序,它有一個Ap.XAML,看起來像這樣。現在如何將TextBlock綁定到app.xaml中定義的ObjectDataProvider資源?

<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我要綁定到ObjectDataProvider的SAG_POK在Ap.xaml。

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

我的問題是,在我的ViewModels之一,我實例化SAG_POK的ObjectDataProvider在App.xaml中與SAG_POK的一個實例。

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

但我想不通的地方把我OnNotifyPropertyChanged(「SAG_POK」)我已經嘗試了不同的情況,但沒有人似乎工作。

以前有人試過這個嗎?請讓我知道任何提示,謝謝提前。

+0

我不明白:爲什麼你使用ObjetcDataProvider何時已經有ViewModels?爲什麼不直接將視圖綁定到ViewModel? – SvenG

+0

由於Objectdataprovider在應用程序中的多個位置使用,並且由於視圖模型在頁面上使用,而不是在窗口中使用。 – ThomasE

+0

你不「設置」這樣的資源。它在被引用時被實例化,並且每次都重用該實例。你試圖使用ODP的方式是奇怪的。爲什麼不只是將您的SAG_POK實例添加爲資源?用以下代碼替換ODP:'<或:SAG_POK x:Key =「SAG_POK」/>' – Will

回答

0

你可以做到這一點

<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是在孔應用YOUT主視圖模型:-)

希望這有助於

+0

謝謝,它工作完美:-) – ThomasE