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
의 목록, 그래서 거기에 템플릿에 문제가 있다고 생각 .. 내가 무엇입니까? 잘못하고있는거야?
작동하지 않습니다 .. 얻을 : 첫 번째 기회 예외 'System.IO.FileNotFoundException'mscorlib.dll에서 발생했습니다 System.Windows.Data 오류 : BindingExpression 경로 오류 : 'WP7SQLiteClient'에서 '제목'속성을 찾을 수 없습니다. Model.MainViewModelItem ''WP7SQLiteClient.Model.MainViewModelItem '(HashCode = 65782377 – Alex
, 즉 바인딩하지 않기 때문에 subTitle 문자열이 나타나지 않습니다. 고정 문자열 – Alex
때문에 viewmodel 속성이 공개되지 않습니다!공개하고 행복하게 만드십시오 :) –