0

プロジェクトの ViewModel 要素が見つかりません。WPF Usercontrol 内に ViewModel を実装しようとしています。ただし、バインディングが正しく機能しておらず、データがないようです。一般的な文字列配列やその他のさまざまなデータを入れて、やり取りする ViewModel を作成しようとしています。

MainWindow.xaml - (Usercontrol 宣言)

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

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;
    }
}
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 は null であるため、ラベルには何も表示されません。あなたのコードから、次のように記述したいと思います:

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

deryck が提案したように、通知用に INotifyPropertyChanged インターフェイスを追加する必要がありますが、最初のバインディングには影響しません。ViewModel のデータは正しいが、UI に入力されていないと思われる場合は、DataContext と Binding を確認する必要があります。

于 2013-06-23T20:25:55.617 に答える