2012-06-13 51 views
0

好的,所以我現在一直在研究幾個小時,仍然無法弄清楚爲什麼我的ViewModel中的數據沒有綁定到我的主頁中的XAML。我甚至開始了一個新項目,並以相同的方式實現它,所以我認爲它可能與名稱空間或我不太熟悉的東西有關。將C#類綁定到WP7的XAML

當我的應用程序啓動時,我創建一個App.cs中的全局ViewModel,我用它將數據綁定到我的XAML視圖。

public HomeViewModel ViewModel { get; private set; } 

private void Application_Launching(object sender, LaunchingEventArgs e) 
{ 
    ViewModel = new HomeViewModel(); 
    (App.Current as App).RootFrame.DataContext = (App.Current as App).ViewModel; 
} 

然後HomeViewModel看起來是這樣的:

public class HomeViewModel : INotifyPropertyChanged 
{  
    /***View Model***/ 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void NotifyPropertyChanged(String info) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(info)); 
     } 
    } 

    public HomeViewModel() 
    { 
     PropertyChanged = new PropertyChangedEventHandler(delegate { }); 
    } 

    public Profile CurrentProfile; /*EDIT: Missing {get;set;} Which is necessary for 
            *any property, including ones below that I 
            *referenced in the XAML 
            */ 

    public string NotificationImage; 

    public ButtonPanelPath UniversalButtonPath; 

    public void setProfile(Profile p) 
    { 
     CurrentProfile = p; 
     NotifyPropertyChanged("CurrentProfile"); 
    } 
    . 
    . 
    ....rest of access methods and properties 

現在,當我的程序運行,我相信,在HomeViewModel中的數據被更新,NotifyPropertyChanged方法被調用每次100%新的領域是「設置」。

而這個類是綁定到RootFrame的嗎?所以我不應該能夠在我的主頁的xaml中訪問這些字段?這是在主電網的堆疊面板的XAML的一部分的例子:

<Border BorderThickness="5" BorderBrush="Aqua" CornerRadius="20"> 
    <StackPanel Name="profileInfo" DataContext="{Binding CurrentProfile}"> 
    <TextBlock Text="{Binding FirstName}" Name="profileName" FontSize="26" 
       FontWeight="Bold" HorizontalAlignment="Center" /> 
    <StackPanel Orientation="Horizontal"> 
     <StackPanel> 
     <TextBlock Text="{Binding Level}" Name="userLevel" FontSize="32" 
        Margin="10,0,0,0"/> 
     <TextBlock Text="{Binding LevelName}" Name="levelName" FontSize="26" 
        Margin="10,0,0,0"/> 
     <TextBlock Text="{Binding PointsNeeded}" Name="pointsBar" 
        Margin="10,0,0,0"/> 
     </StackPanel> 
     <Image x:Name="levelIcon" Source="{Binding PictureUrl}" 
      Margin="15,0,0,0"/> 
    </StackPanel> 
    </StackPanel> 
</Border> 

所以在這裏等級,LevelName,PointsNeeded和PictureUrl在檔案(或CurrentProfile所有的公共領域是檔案的具體實例我參考了)。我試過配置文件[場],但也沒有工作。如果有人能告訴我我錯過了什麼來完成綁定,那將不勝感激。

順便說命名空間是如下如果這意味着什麼
-MainPage是MyApp.src.pages
-APP是MyApp的
-HomeViewModel是MyApp.src.classes

謝謝提前爲您提供有用的解決方案/意見,如果您需要更多的數據/信息,請提問。

+0

好吧,所以我發現有一個很大的問題,就是當你聲明屬性這樣做的時候:「public Profile CurrentProfile {get; set;}」,而不是如上所述。對於您將在XAML中使用INotifyPropertyChanged引用的所有其他屬性,請執行相同的操作。 – methodMan

+0

很高興您解決了您的問題 - 如果您找出實際造成問題的原因,請發表回覆! –

+0

在那裏,我在代碼部分添加了編輯,這是你的意思嗎? – methodMan

回答

0

您正在尋找的綁定是{Binding Proptery.SubProperty}。

所以在你的情況下,例如{Binding CurrentProfile.Level}。

您正在DataContext中擁有一個「HomeViewModel」實例,因此您可以訪問它的所有參數。如果存在複雜類型作爲屬性,則必須訪問屬性(複雜類型的實例而不是類型)以訪問其「子」屬性。

希望它有幫助。

+1

感謝您的回覆。我嘗試使用{Binding Property.SubPropery},但它不起作用。這就是爲什麼我試圖將CurrentProfile綁定到StackPanel,然後從內部訪問子屬性,但顯然這也不起作用。還有什麼想法? – methodMan

+0

我覺得它甚至沒有正式規定它會綁定到這個類,有沒有一種快速簡單的方法來測試,以確保(除了我已經在做什麼)也許在調試器或什麼? – methodMan

+0

你可以綁定HomeViewModel的直接屬性嗎?例如「NotificationImage」。你必須創建屬性,公共領域不工作,因爲我知道... –