簡體   English   中英

將C#類綁定到WP7的XAML

[英]Binding a C# class to XAML for WP7

好的,所以我已經花了幾個小時了,仍然不知道為什么我的ViewModel中的數據沒有綁定到我的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

現在,當我的程序運行時,我100%確保每次“設置”新字段時,HomeViewModel中的數據都將更新,並且將調用NotifyPropertyChanged方法。

此類綁定到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>

因此,這里的Level,LevelName,PointsNeeded和PictureUrl都是Profile(或CurrentProfile,這是我正在引用的Profile的特定實例)中的所有公共字段。 我嘗試了Profile。[field],但是那也不起作用。 如果有人能告訴我完成綁定所缺少的內容,將不勝感激。

順便說一句,如果這意味着任何東西,則命名空間如下
-MainPage在MyApp.src.pages中
-App在MyApp中
-HomeViewModel在MyApp.src.classes中

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

您要查找的綁定是{Binding Proptery.SubProperty}。

因此,以您的情況為例,例如{Binding CurrentProfile.Level}。

您在DataContext中具有“ HomeViewModel”的實例,因此可以訪問其所有屬性。 如果存在復雜類型作為屬性,則必須訪問該屬性(不是該類型的復雜類型的實例)才能訪問其“子”屬性。

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM