2012-08-25 75 views
0

在下面的第一個模塊中,我故意錯過了命名的綁定語句之一,所以我可以比較我的第二個模塊。區別在於'ag2.item ...'行中未找到的屬性。
item是我的模特。
MVVM綁定錯誤?

在塊2中,您可以看到它指向我的視圖模型(ag2.viewModel.itemViewModel)。

我需要在我的XAML或我的代碼隱藏中做些什麼才能讓它指向我的類而不是viewmodel?

1座:

BindingExpression path error: 'itemModel1' property not found on 'ag2.item, ag2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. BindingExpression: Path='itemModel1' DataItem='ag2.item, ag2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'; target element is 'Windows.UI.Xaml.Controls.TextBlock' (Name='null'); target property is 'Text' (type 'String')

2座:

BindingExpression path error:'itemModel' property not found on 'ag2.viewModel.itemViewModel, ag2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. BindingExpression: Path='itemModel' DataItem='ag2.viewModel.itemViewModel, ag2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'; target element is 'Windows.UI.Xaml.Controls.TextBlock' (Name='null'); target property is 'Source' (type 'String')

代碼後面塊2:

itemViewModel VM = new itemViewModel((Int32)navigationParameter); 
    DataContext = VM; 

我也請注意,在塊1中,我正在綁定到設置了ItemSource="{Binding item}"的GridView。

塊2我建立使用該網格和我的TextBlocks UI:Text="{Binding Path=itemModel}"

更新:在努力嘗試,以獲得更好的理解。我將代碼放在那裏:這裏是XAML,下面是ViewModel,下面是我的模型...我是MVVM的新手,所以我真的不知道我做錯了什麼。任何幫助是極大的讚賞。

XAML:

<common:LayoutAwarePage 
    x:Name="pageRoot" 
    x:Class="autoGarage2.VehicleItemDetailPage" 
    IsTabStop="false" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:common="using:autoGarage2.Common" 
    xmlns:local="using:autoGarage2" 
    xmlns:data="using:autoGarage2" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"> 


    <!-- 
     This grid acts as a root panel for the page that defines two rows: 
     * Row 0 contains the back button and page title 
     * Row 1 contains the rest of the page layout 
    --> 
    <Grid Style="{StaticResource LayoutRootStyle}"> 

     <Grid.RowDefinitions> 
      <RowDefinition Height="140"/> 
      <RowDefinition Height="2*"/> 
     </Grid.RowDefinitions> 

     <!-- Back button and page title --> 
     <Grid 
     Style="{StaticResource LayoutRootStyle}" Grid.Row="0"> 

      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="Auto"/> 
       <ColumnDefinition Width="*"/> 
      </Grid.ColumnDefinitions> 
      <Button x:Name="backButton" Click="GoBack" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource BackButtonStyle}"/> 
      <TextBlock x:Name="pageTitle" Text="{StaticResource AppName}" Style="{StaticResource PageHeaderTextStyle}" Grid.Column="1"/> 
     </Grid> 



     <Grid Grid.Row="1"> 


      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="Auto"/> 
       <ColumnDefinition Width="*"/> 
      </Grid.ColumnDefinitions> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="3*"/> 
       <RowDefinition Height="1*"/> 
      </Grid.RowDefinitions> 


       <StackPanel Grid.Row="0" Orientation="Horizontal" Width="auto" Margin="50,0,0,0" VerticalAlignment="Top" > 
        <Grid HorizontalAlignment="Left" Width="250" Height="250"> 
         <Border Background="White" BorderBrush="CornflowerBlue" BorderThickness="1"> 
           <Image Source="{Binding Image}" Margin="50"/> 
          </Border> 
          <StackPanel VerticalAlignment="Bottom" Background="CornflowerBlue"> 
           <StackPanel Orientation="Horizontal" DataContext="{Binding vehicles}"> 
           <TextBlock Text="{Binding VehicleMake}" Foreground="{StaticResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource PageSubheaderTextStyle}" Margin="5"/> 
           <TextBlock Text="{Binding VehicleModel}" Foreground="{StaticResource ListViewItemOverlaySecondaryForegroundThemeBrush}" Style="{StaticResource PageSubheaderTextStyle}" Margin="5"/> 
           </StackPanel> 
          </StackPanel> 
         </Grid> 
       </StackPanel> 
        <StackPanel Grid.Row="0" Grid.Column="1"> 
         <Grid Margin="20,0,0,20"> 
          <Grid.RowDefinitions> 
           <RowDefinition Height="1*"/> 
           <RowDefinition Height="1*"/> 
           <RowDefinition Height="1*"/> 
           <RowDefinition Height="1*"/>         
           <RowDefinition Height="1*"/>         
           <RowDefinition Height="1*"/> 
           <RowDefinition Height="1*"/> 
           <RowDefinition Height="1*"/> 
           <RowDefinition Height="1*"/>         
           <RowDefinition Height="1*"/>  
           <RowDefinition Height="1*"/> 
           <RowDefinition Height="1*"/> 
           <RowDefinition/>         
          </Grid.RowDefinitions> 

          <Grid.ColumnDefinitions> 
           <ColumnDefinition Width="1*"/> 
           <ColumnDefinition Width="1*"/> 
           <ColumnDefinition Width="2*"/> 
          </Grid.ColumnDefinitions> 






          <TextBlock Text="Vehicle Make:" Grid.Row="0" Grid.Column="0" FontSize="25" Foreground="Black" /> 
          <TextBox Text="{Binding Path=VehicleMake}" Grid.Row="0" Grid.Column="1" FontSize="25" BorderBrush="CornflowerBlue" BorderThickness="1"/> 

          <TextBlock Text="Vehicle Model:" FontSize="25" Foreground="Black" Grid.Row="1" Grid.Column="0"/> 
          <TextBox Text="{Binding VehicleModel}" Grid.Row="1" Grid.Column="1" FontSize="25" BorderBrush="CornflowerBlue" BorderThickness="1"/>        

          <TextBlock Text="Vehicle Year:" FontSize="25" Foreground="Black" Grid.Row="2" Grid.Column="0"/> 
          <TextBox Text="{Binding VehicleYear}" Grid.Row="2" Grid.Column="1" FontSize="25" BorderBrush="CornflowerBlue" BorderThickness="1"/> 

          <TextBlock Text="License Plate:" FontSize="25" Foreground="Black" Grid.Row="3" Grid.Column="0"/> 
          <TextBox Text="" Grid.Row="3" Grid.Column="1" FontSize="25" BorderBrush="CornflowerBlue" BorderThickness="1"/> 

          <TextBlock Text="VIN #" FontSize="25" Foreground="Black" Grid.Row="4" Grid.Column="0"/> 
          <TextBox Text="" Grid.Row="4" Grid.Column="1" FontSize="25" BorderBrush="CornflowerBlue" BorderThickness="1" />  

          <TextBlock Text=" Current Mi/Km" FontSize="25" Foreground="Black" Grid.Row="5" Grid.Column="0"/> 
          <TextBox Text="" Grid.Row="5" Grid.Column="1" FontSize="25" BorderBrush="CornflowerBlue" BorderThickness="1"/>    

          <TextBlock Text="" FontSize="25" Foreground="Black" Grid.Row="6" Grid.ColumnSpan="2"/> 

          <TextBlock Text="Last Oil Change" FontSize="25" Foreground="Black" Grid.Row="7" Grid.Column="0"/> 
          <TextBox Text="" Grid.Row="7" Grid.Column="1" FontSize="25" BorderBrush="CornflowerBlue" BorderThickness="1"/>    

          <TextBlock Text="Last Oil Change Mi/Km" FontSize="25" Foreground="Black" Grid.Row="8" Grid.Column="0"/> 
          <TextBox Text="" Grid.Row="8" Grid.Column="1" FontSize="25" BorderBrush="CornflowerBlue" BorderThickness="1"/> 

          <TextBlock Text="" FontSize="25" Foreground="Black" Grid.Row="9" Grid.ColumnSpan="2"/> 

          <TextBlock Text="Reminder Mi/Km" FontSize="25" Foreground="Black" Grid.Row="10" Grid.Column="0"/> 
          <TextBox Text="" Grid.Row="10" Grid.Column="1" FontSize="25" BorderBrush="CornflowerBlue" BorderThickness="1"/>  

          <TextBlock Text="Reminder Month(s)" FontSize="25" Foreground="Black" Grid.Row="11" Grid.Column="0"/> 
          <TextBox Text="" Grid.Row="11" Grid.Column="1" FontSize="25" BorderBrush="CornflowerBlue" BorderThickness="1"/>       

         </Grid>      
       </StackPanel> 

      </Grid> 

    </Grid> 
</common:LayoutAwarePage> 

視圖模型:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Collections.ObjectModel; 
using Windows.Foundation.Collections; 
using System.IO; 


namespace autoGarage2.viewModel 
{ 
    class vehicleViewModel 
    { 
     private IList<vehicle> m_vehicles; 
     private IList<vehicle> m_vehicleItem; 

     public IList<vehicle> vehicles 
     { 
      get { return m_vehicles; } 
      set { m_vehicles = value; } 
     } 

     public IList<vehicle> vehicleItem 
     { 
      get { return m_vehicleItem; } 
      set { m_vehicleItem = value; } 
     } 

     private IList<vehicle> getVehicleDetail(Int32 vId) 
     { 
      var vehicleItem = 
       from v in vehicles 
       where v.VehicleId == vId 
       select v; 
      if (vId > 0) 
      { 
       //vehicles.Clear(); 
       m_vehicles = new List<vehicle>(); 
       foreach (var item in vehicleItem) 
       { 
        m_vehicles = new List<vehicle> 
        { 
         new vehicle(item.VehicleId, item.VehicleMake.ToString(), item.VehicleModel.ToString(), item.VehicleYear, item.Image.ToString()) 
        }; 
        //vehicle myVehicle = new vehicle(item.VehicleId, item.VehicleMake.ToString(), item.VehicleModel.ToString(), item.VehicleYear, item.Image.ToString()); 
        //m_vehicles.Add(myVehicle); 
       } 

      } 

      return m_vehicles; 
     } 

     public vehicleViewModel(Int32 vId) 
     { 
       m_vehicles = new List<vehicle> 
       {   
       new vehicle(1, "Mazda", "3", 2011, "Assets/car2.png"), 
       new vehicle(2, "Chevy", "Tahoe", 2004, "Assets/jeep1.png"), 
       new vehicle(3, "Honda", "Goldwing", 2007 ,"Assets/moto1.png") 
       }; 

       if (vId > 0) 
       { 
        //m_vehicles = new List<vehicle>(); 
        //m_vehicles = 
        //getVehicleDetail(vId); 
        m_vehicles = new List<vehicle> 
        {   
        new vehicle(2, "Chevy", "Tahoe", 2004, "Assets/jeep1.png"), 
        }; 

       } 
     } 



     #region dbCode 

     //string dbName = "vehicle.db"; 


     //var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, dbName);  
     //using (var db = new SQLite.SQLiteConnection(dbPath))  
     // { 
     //  var list = db.Table<vehicle>().ToList(); 
     //  m_vehicles = new List<vehicle>(); 
     //  for (Int32 i = 0; i < list.Count; i++) 
     //  { 
     //   //m_vehicles.Add(db.Table<vehicle>().ToList()); 
     //  } 
     // } 




     //foreach (vehicle item in m_vehicles) 
     //{ 
     // AllItems.Add(item); 
     //}  
     #endregion 

    } 
} 

型號:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
//using SQLite; 
using Windows.UI.Xaml.Media; 
using Windows.UI.Xaml.Media.Imaging; 

namespace autoGarage2 
{ 
    class vehicle : autoGarage2.Common.BindableBase 
    { 
     public vehicle() 
     { 
     } 

     public vehicle(string imagePath) 
     { 
      this._imagePath = imagePath; 
     } 
     public vehicle(Int32 vId, string vMake, string vModel, Int16 vYear, string imagePath) 
     { 
      this.m_vehicleID = vId; 
      this.m_vehicleMake = vMake; 
      this.m_vehicleModel = vModel; 
      this.m_vehicleYear = vYear; 
      this.m_vehicleName = vMake + " " + vModel; 
      this._imagePath = imagePath; 
     } 


     private Int32 m_vehicleID; 

     private String m_vehicleMake; 
     private String m_vehicleModel; 
     private Int16 m_vehicleYear; 
     private string m_vehicleName; 


     private ImageSource _image = null; 
     private String _imagePath = null; 
     private static Uri _baseUri = new Uri("ms-appx:///"); 

     //[AutoIncrement, PrimaryKey] 
     public Int32 VehicleId 
     { 
      get 
      { 
       return m_vehicleID; 
      } 
      set 
      { 
       m_vehicleID = value; 
       OnPropertyChanged("VehicleId"); 
      } 
     } 

     public String VehicleMake 
     { 
      get 
      { 
       return m_vehicleMake; 
      } 
      set 
      { 
       m_vehicleMake = value; 
       OnPropertyChanged("VehicleMake"); 
      } 
     } 

     public String VehicleModel 
     { 
      get 
      { 
       return m_vehicleModel; 
      } 
      set 
      { 
       m_vehicleModel = value; 
       OnPropertyChanged("VehicleModel"); 
      } 
     } 
    public Int16 VehicleYear 
    { 
     get 
     { 
      return m_vehicleYear; 
     } 
     set 
     { 
      m_vehicleYear = value; 
      OnPropertyChanged("VehicleYear"); 
     } 
    } 

    public string VehicleName 
    { 
     get 
     { 
      return m_vehicleName; 
     } 
     set 
     { 
      m_vehicleName = value; 
      OnPropertyChanged("VehicleName"); 
     } 
    } 

    public ImageSource Image 
    { 
     get 
     { 
      if (this._image == null && this._imagePath != null) 
      { 
       this._image = new BitmapImage(new Uri(vehicle._baseUri, this._imagePath)); 
      } 
      return this._image; 
     } 

     set 
     { 
      this._imagePath = null; 
      this.SetProperty(ref this._image, value); 
     } 
    } 

    public void SetImage(String path) 
    { 
     this._image = null; 
     this._imagePath = path; 
     this.OnPropertyChanged("Image"); 
    } 
} 

}

+0

這是有點不清楚你在做什麼,你想達到什麼。你的視圖模型的結構是什麼?你的XAML的結構是什麼?你想綁定到itemModel嗎? itemModel在哪裏定義? –

+0

請查看更新後的問題。我添加了一切。如果你能幫助它將非常感謝 – webdad3

+0

請也澄清你遇到的問題。上面的問題似乎引用了ItemModel的綁定錯誤,但是沒有綁定到您的XAML中的ItemModel。哪個'TextBox'導致錯誤,你想要綁定到什麼?你的DataContext在哪裏設置了視圖? –

回答

0

我沒能看到它,所以我能夠在週末看到我的數據。基本上我注意到,我得到一個綁定錯誤,指出它無法在我的autoGarage2.vehicles列表中找到該屬性。因此,對於我的笑容,我以這樣的方式開頭:

{Binding vehicles[0].vehicleModel} 

我下次運行它時,數據就在那裏。在考慮了更多之後,我決定創建一個單一的車輛對象,而不是填充列表對象,而是僅填充單個車輛屬性。現在我正在做這樣的事情:

{Binding vehicleSingle.vehicleModel} 

謝謝你的幫助。我想這只是MVVM如何在XAML中工作的細微差別......

1

這聽起來像你想你的TextBlock數據綁定到不同的對象,而不是ViewModel(這是您的窗口/控件的DataContext)。如果這是正確的,則需要將TextBlockDataContext或父Grid設置爲要DataBind所對應的對象。

DataContext用於確定控件的DataBind路徑,並沿着可視樹繼承。因此,如果您的DataContext設置爲MyViewModel,並且您使用Text="{Binding Path=itemModel}",則綁定路徑將爲MyViewModel.itemModel

如果您不想在綁定路徑中包含MyViewModel,則需要更改相關控件的DataContext或包含控件的DataContext。對於MVVM,這通常是通過將另一個對象作爲ViewModel的屬性公開來完成的。所以,如果你有MyViewModel屬性ItemModel:

public class ItemModel 
{ 
    public string Property1 { get; } 
    public string Property2 { get; } 
} 

public class MyViewModel 
{ 
    public ItemModel ItemModel { get; private set; } 
} 

那麼你的XAML看起來是這樣的(假設MyViewModel是父窗口/控件的DataContext的)。

<Grid Grid.Row="1" DataContext="{Binding ItemModel}"> 
    <Grid.RowDefinitions> 
     <RowDefinition/> 
     <RowDefinition/> 
    </Grid.RowDefinitions> 
    <TextBlock Text="{Binding property1}"/> 
    <TextBlock Text="{Binding Property2}" Grid.Row="1"/> 
</Grid> 

而這兩個文本框將綁定到ItemModel對象的Property1和Property2。

+0

請參閱我更新的問題。希望我的代碼能夠幫助識別我做錯了什麼。 – webdad3

1

您似乎試圖綁定到列表,但您沒有在您的XAML中使用任何ItemsControl。你可能應該使用類似ListView的東西,將它的ItemsSource綁定到你的車輛或vehicleItem列表中,使用ItemTemplate/DataTemplate來定義集合中每個項目的外觀,如果你的集合更改或者引發INotifyPropertyChanged.PropertyChanged通知,則使用ObservableCollection。你的收藏等。否則,我會建議閱讀一些關於綁定ItemsControls或關於XAML的一般書籍,如Adam Nathan的WPF Unleashed。看來你正在設置你的m_vehicles,只是在下一個語句中用新的替換它。另外 - 你正在設置StackPanel的DataContext到你的列表中,這是允許的,但它不起作用,因爲你的StackPanel的元素的DataContext仍然是整個列表而不是它的項目,這是你得到的使用ItemsControl。

+0

謝謝你的回答。我實際上已經開始工作了。我會發布我以後做的事情。 – webdad3