2011-10-28 2 views
1

xaml을 내 viewmodel과 바인딩 할 수 없습니다. 내 viewModel에 Recepie에 대한 데이터를 보유하는 INotifyPropertyChanged 클래스의 ObservableCollection이 있습니다. recepies의 목록을 보유하고link viewmodel to

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)); 
      } 
     } 
    } 
} 

그리고 내 뷰 모델 : 여기 내 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)); 
       } 
      } 
     } 

} 

을 (그것은 프로젝트의 루트에있어, 뷰 모델은 뷰 모델에 폴더와 모델 난 내 목록 상자는 다음과 같이 선언 한) 모델 폴더에 있습니다

<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}

같은 것들을 사용합니다. 뷰 모델을 xaml과 올바르게 연결할 수 있습니까? 내 템플릿은 다음과 같습니다

UPDATE :

<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

내가 내 목록 상자의 코드를 변경했습니다 : 없음 템플릿

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

, 내가 얻을 [project_name].Model.MainViewModelItem의 목록, 그래서 거기에 템플릿에 문제가 있다고 생각 .. 내가 무엇입니까? 잘못하고있는거야?

답변

2

ListBox를 데이터에 바인딩해야합니다. 그래서, 이것은 당신을 위해 일해야합니다.

<ListBox x:Name="recepiesList" ItemsSource="{Binding recepiesList}" ItemTemplate="{StaticResource ListViewModelTemplate}" /> 
+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를 사용하지 않는다면 가져 오기없이 위의 동일한 코드를 사용할 수 있지만 뷰를 만들면 viewmodel 클래스의 새 인스턴스를 할당해야합니다.

+0

내 viewmodel 클래스의 새 인스턴스를 어떻게 할당 할 수 있습니까? 나는 당신이 무엇을 의미하는지 모르겠습니다. – Alex

+0

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

+0

UPDATE2를 추가했습니다. – Alex