2013-06-23 70 views
0

我的項目ViewModel元素未找到。我試圖在我的WPF用戶控件中實現一個ViewModel。但是,綁定工作不正常,似乎沒有數據。我試圖創建一個ViewModel進行交互,將通用字符串數組放入以及各種其他位數據。未找到ViewModel數據

MainWindow.xaml - (用戶控件聲明)

<panels:FilterLister Grid.Column="0" x:Name="filter1FilterLister" /> 

MainWindows.cs - (在構造函數中,調用用戶控件

filter1FilterLister.Initialise(typeof(Genre)); 

FilterListViewModel.cs

public class FilterListViewModel 
{ 
    MyEntities context = new MyEntities(); 
    ObservableCollection<string> entries = new ObservableCollection<string>(); 

    public Type SelectedType; 
    private string p_TypeName; 
    public string TypeName 
    { 
     get { return p_TypeName; } 
     set { 
      //p_TypeName = value; 
      p_TypeName = SelectedType.Name.ToString(); 
     } 
    } 

    public FilterListViewModel() { } 

    public FilterListViewModel(Type selectedType) 
    { 
     if (selectedType == typeof(Artist)) 
     { 
      returnedArray = Artist.ReturnArtistNames(context); 
     } 

     // put together ObservableCollection 
     foreach (var str in returnedArray) 
     { 
      entries.Add(str); 
     } 

     SelectedType = selectedType; 
    } 
} 

FilterLister。 xaml

<Label Name="labelToBind" Content="{Binding TypeName}" Grid.Row="0" /> 

FilterLister.cs

public partial class FilterLister : UserControl 
{ 
    FilterListViewModel filterListViewModel; 
    private MyEntities context; 

    public FilterLister() 
    { 
     InitializeComponent(); 
     context = new MyEntities(); 
    } 

    public void Initialise(Type objectType) 
    { 
     filterListViewModel = new FilterListViewModel(objectType); 
     this.DataContext = filterListViewModel; 
    } 
} 

回答

1

基於您的代碼,typeName爲null,所以你看到了什麼標籤。從你的代碼,我想你想描述,如:

public string TypeName 
{ 
    get{ return SelectedType.Name.ToString();} 
} 

正如德里克建議,應添加INotifyPropertyChanged接口的通知,但它不應該影響到第一次結合。如果您認爲ViewModel的數據正確但未在UI上填充,則應檢查DataContext和Binding。

1

你已經錯過了實現您的視圖模型INotifyPropertyChanged接口,它需要綁定的屬性可以發送「刷新信息」的用戶界面。

這裏是接口,以及如何實現這一點: http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

public class FilterListViewModel : INotifyPropertyChanged 
{ 
MyEntities context = new MyEntities(); 
ObservableCollection<string> entries = new ObservableCollection<string>(); 

public Type SelectedType; 
private string p_TypeName; 
public string TypeName 
{ 
    get { return p_TypeName; } 
    set { 
     //p_TypeName = value; 
     p_TypeName = SelectedType.Name.ToString(); 
    NotifyPropertyChanged(); 

    } 
} 

public FilterListViewModel() { } 

public FilterListViewModel(Type selectedType) 
{ 
    if (selectedType == typeof(Artist)) 
    { 
     returnedArray = Artist.ReturnArtistNames(context); 
    } 

    // put together ObservableCollection 
    foreach (var str in returnedArray) 
    { 
     entries.Add(str); 
    } 

    SelectedType = selectedType; 
} 

private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
}