2011-10-28 20 views
1

xamlを私のviewmodelにバインドすることができません。私は自分のviewModelにRecepieに関するデータを保持するINotifyPropertyChangedクラスのObservableCollectionを持っています。 recepiesのリストを保持link viewmodel to view

namespace WP7SQLiteClient.Model 
{ 
    public class MainViewModelItem : INotifyPropertyChanged 
    { 
     string _title, _subTitle, _imageUriPath; 

     string title 
     { 

      get 
      { 
       return _title; 
      } 
      set 
      { 
       _title = value; 
       NotifyPropertyChanged("title"); 
      } 
     } 
     string subTitle 
     { 
      get 
      { 
       return _subTitle; 
      } 
      set 
      { 
       _subTitle = value; 
       NotifyPropertyChanged("subTitle"); 
      } 
     } 
     string imageUriPath 
     { 
      get 
      { 
       return _imageUriPath; 
      } 
      set 
      { 
       _imageUriPath = value; 
       NotifyPropertyChanged("imageUriPath"); 
      } 
     } 

     public MainViewModelItem(string title, string subtitle, string imageuripath) 
     { 
      this.title = title; 
      this.subTitle = subtitle; 
      this.imageUriPath = imageuripath; 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

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

そして、私のViewModel:ここに私のRecepieクラスである私のMainPage.xamlをで

namespace WP7SQLiteClient.ViewModel 
{ 

     public class PanoramaViewModel : INotifyPropertyChanged 
     { 
      public ObservableCollection<MainViewModelItem> _recepiesList; 


      public ObservableCollection<MainViewModelItem> recepiesList 
      { 
       get 
       { 
        return _recepiesList; 
       } 

       set 
       { 
        _recepiesList = value; 
        NotifyPropertyChanged("recepiesList"); 
       } 
      } 

      public PanoramaViewModel() 
      { 
       this.recepiesList = new ObservableCollection<MainViewModelItem>(); 

      } 

      public bool IsDataLoaded 
      { 
       get; 
       private set; 
      } 

      public void LoadData() 
      { 
       this.recepiesList.Add(new MainViewModelItem("Classics", "", "")); 
       this.recepiesList.Add(new MainViewModelItem("Perfect Pasta", "", "")); 
       this.recepiesList.Add(new MainViewModelItem("Favorites", "", "")); 
       this.recepiesList.Add(new MainViewModelItem("Snacks & Antipasti", "", "")); 
       this.recepiesList.Add(new MainViewModelItem("Desserts", "", "")); 
       this.recepiesList.Add(new MainViewModelItem("3 minutes recipes", "", "")); 

       this.IsDataLoaded = true; 
      } 


      private string _sampleProperty = "Sample Runtime Property Value"; 
      /// <summary> 
      /// Sample ViewModel property; this property is used in the view to display its value using a Binding 
      /// </summary> 
      /// <returns></returns> 
      public string SampleProperty 
      { 
       get 
       { 
        return _sampleProperty; 
       } 
       set 
       { 
        if (value != _sampleProperty) 
        { 
         _sampleProperty = value; 
         NotifyPropertyChanged("SampleProperty"); 
        } 
       } 
      } 




      public event PropertyChangedEventHandler PropertyChanged; 

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

} 

を(それがプロジェクトのルートにあります、ViewModelにはViewModelにですフォルダとモデルがモデルフォルダにある)私は私のリストボックスは、次のように宣言している:

<ListBox x:Name="recepiesList" ItemTemplate="{StaticResource ListViewModelTemplate}" > 

       </ListBox> 

テンプレートはApp.xamlに位置し、確かに正しいです。

public static PanoramaViewModel viewModel = null; 

     public static PanoramaViewModel ViewModel 
     { 
      get 
      { 
       // Delay creation of the view model until necessary 
       if (viewModel == null) 
        viewModel = new PanoramaViewModel(); 

       return viewModel; 
      } 
     } 
public MainPage() 
     { 

      InitializeComponent(); 

      ViewModel.LoadData(); 
      DataContext = ViewModel; 

     } 

をしかし、それは動作しません、また、デバッガでエラーが発生します:それは私が使用してページとビューモデルをリンクしようMainPage.csで{Binding title}

のようなものを使用しています。 viewmodelとxamlを正しくリンクするにはどうしたらいいですか?私は取得するには、テンプレートなしで

<ListBox x:Name="recepiesList" ItemsSource="{Binding recepiesList}" >    </ListBox> 

<DataTemplate x:Key="ListViewModelTemplate"> <!-- for recent recepies--> 

      <Grid Width="400" Height="80" VerticalAlignment="Center"> 

       <StackPanel Orientation="Vertical"> 
        <Border CornerRadius="0" x:Name="brdTesat" BorderBrush="Black" BorderThickness="1" Width="80" Height="80"> 

        <Border.Background> 
         <ImageBrush x:Name="backgroundImaageBrush" Stretch="Fill"> 

          <ImageBrush.ImageSource> 

            <BitmapImage x:Name="bmapBackground" UriSource="{Binding imageUriPath}" > 
           </BitmapImage> 

          </ImageBrush.ImageSource> 
         </ImageBrush> 
        </Border.Background> 
       </Border> 
        <StackPanel> 
        <TextBlock TextAlignment="Left" Margin="7,4,4,4" Text="{Binding title}" TextWrapping="Wrap"></TextBlock> 
         <TextBlock TextAlignment="Left" Margin="7,4,4,4" Text="DA" TextWrapping="Wrap"></TextBlock> 
        </StackPanel> 
       </StackPanel> 

      </Grid> 
     </DataTemplate> 

UPDATE 2

私は私のリストボックスのコードを変更しました:私のテンプレートは次のようになります

UPDATE [project_name].Model.MainViewModelItemのリストは、テンプレートに問題があると思います。私は何ですか?間違っている?

答えて

2

ListBoxをデータにバインドする必要があります。だから、これはあなたのために働くはずです。

​​
+0

が動作しません。私は得る:最初のチャンス例外 'System.IO.FileNotFoundException' mscorlib.dllで発生しました System.Windows.Dataエラー:BindingExpressionパスエラー: 'WP7SQLiteClientで'タイトル 'プロパティが見つかりません。 Model.MainViewModelItem '' WP7SQLiteClient.Model.MainViewModelItem '(HashCode = 65782377 – Alex

+0

つまり、私がバインディングしていないためにsubTitle文字列が表示されない、静的文字列を返します – Alex

+1

viewmodelのプロパティが公開されていないためです。それらを公開して幸せにしてください:) –

0

私たちは、プロジェクト内のMEFを使用していると我々はView.xaml.csで次のコードでのビューモデルをリンクアップ:これは、カタログが作成されたときに、インポートを満たすことを可能にする

[Import] 
public ConnectionStringSetupViewModel ViewModel 
{ 
    get { return DataContext as ConnectionStringSetupViewModel; } 
    set { DataContext = value; } 
} 

。 MEFを使用していない場合は、上記のコードをインポートせずに使用できますが、ビューを作成する際には、ビューモデルクラスの新しいインスタンスを割り当てる必要があります。

+0

私はどのように私のviewmodelクラスの新しいインスタンスを割り当てることができますか?私はあなたが何を意味するか分からない – Alex

+0

PanaromaView p = new PanaromaView(); p.ViewModel = new PanaromaViewModel(); –

+0

私はUPDATE2を追加しました。見てください – Alex

関連する問題