0

找不到我的项目 ViewModel 元素。我正在尝试在我的 WPF 用户控件中实现 ViewModel。但是,绑定无法正常工作,并且似乎没有数据。我正在尝试创建一个 ViewModel 进行交互,将通用字符串数组放入以及各种其他数据位。

MainWindow.xaml -(用户控件声明)

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

MainWindows.cs - (在构造函数中,调用 usercontrol

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" />

过滤器列表器.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;
    }
}
4

2 回答 2

1

您错过了在 ViewModel 中实现 INotifyPropertyChanged 接口,绑定的属性需要可以向 UI 发送“刷新消息”。

这是接口,以及如何实现它: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));
        }
    }
 }
于 2013-06-23T19:59:59.677 回答
1

根据您的代码,TypeName 为空,因此您在标签上看不到任何内容。从您的代码中,我认为您想描述如下:

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

正如deryck建议的那样,您应该为通知添加INotifyPropertyChanged接口,但它不应影响第一次绑定。如果您认为 ViewModel 的数据正确但未在 UI 上填充,则应检查 DataContext 和 Binding。

于 2013-06-23T20:25:55.617 回答