簡體   English   中英

MVVM 樹狀視圖 wpf(綁定?)

[英]MVVM treeview wpf(binding?)

對不起,菜鳥問題。 也許這不是一個問題 - 但我需要一個教程示例/我正在嘗試編寫一個模型任務,以便在 wpf 中使用 mvvm 模式“得心應手”。 我決定寫一些類似圖書管理員助手的東西。 那是一個簡單的 wpf 窗口,帶有一個組合框(用於選擇這本書是幻想書還是科幻書)、文本框(用於書名輸入)、按鈕添加和一個樹狀視圖,例如:

 Books
  ------>ScienceFic
        ----->bookName1
        ----->bookName2
  ------->Fantasy
        ------>bookName3

而且我在綁定和分離視圖模型方面遇到了一些困難。 你可以幫幫我嗎?

public enum LibraryType
{
    ScienceFic,
    Fantasy
}
    Class Model: INotifyPropertyChanged
    {
     private int bookId;
     private string bookName;
     private LibraryType bookType;
     public event PropertyChangedEventHandler PropertyChanged;
        //....and for all this fields  I have used a INotifyPropertyChanged as 
    /*
    http://stackoverflow.com/questions/1315621/implementing-inotifypropertychanged-does-a-better-way-exist
    */
    }

在這一點上,我認為 MODEL 已經完成了。

現在關於 VIEW:我只需打開 MainWindow 的編輯器並手動添加按鈕、文本框、組合框和樹視圖。 添加后

comboBox1.ItemsSource = Enum.GetValues(typeof(LibraryType)).Cast<LibraryType>();
 comboBox1.SelectedIndex = 0;

我已經用正確類型的書籍初始化了我的組合)))但是((,我已經在代碼中完成了這個,而不是在 XAML 中 - 這是否適合 MVVM 綁定?(第一個問題)接下來,我綁定了(我想)我的文本框文本到 ViewModel 中的當前書名屬性(我已經在 XAML 中這樣做了):

 <TextBox Text="{Binding Path=CurrentBookName}"/>

這是正確的嗎? (第二個問題)

最后,VIEWMODEL

 Class ViewModel
{
  private string currentBookName;
  private LibraryType selectedBookType;
  public event PropertyChangedEventHandler PropertyChanged;
  //also properties and  OnPropertyChanged/SetField functions like in Model class

  //and finally I have created two lists for book storing
  private List<Model> sfStorage = new List<Model>();
  private List<Model> fantasyStorage = new List<Model>();
  //and simple getters for this two lists, like:
  public List<Model> GetSFStorage
  {
   get{return this.sfStorage;}
  }
  //and a simple function for addition of new books
  //that I plan to call when AddButton pressed
  public void Addition(???)
        {
            if (!string.IsNullOrEmpty(currentBookName))
            {
                //? I have add CurrentBookName in XAML of View (see above)
                //?but is it enough to have a property of CurrentBookType
                //to grant that a valide value from combobox will be here?
                var modelNewBook = new Model(CurrentBookName,CurrentBookType);
                switch (selectedBookType)
                {
                    case LibraryType.ScienceFic:
                        sfStorage.Add(modelNewBook);
                        break;
                    case LibraryType.Fantasy:
                        fantasyStorage.Add(modelNewBook);
                        break;
                    default:
                        return;
                }
            }
}

現在我離成功只有幾步之遙(可能是)但是,我如何在 xaml 或代碼中將 treeView 與這兩個列表連接起來以及如何在按下 Addbutton 時調用 Addition 函數(第三個也是主要問題)? 也許,這是菜鳥問題,但我真的需要一個樣本。 而且,(也許)這個問題對其他試圖理解 MVVM 和綁定的人會有所幫助。

以下代碼是對您的應用程序的快速破解。 我也知道你有樹視圖的問題,所以我只向樹視圖顯示代碼段落,我在視圖模型的構造函數中填充我的書籍列表以獲取一些數據

模型:

internal class Book : NotifyPropertyChangedBase
{
    private string name;
    private BookType type;
    public BookType Type
    {
       get => this.type;
    }
    set
    {
        this.SetProperty( ref this.type,value );
    }
}

public string Name
{
    get => return this.name;
    set
    {
        this.SetProperty( ref this.name,value );
    }
}

視圖模型:

private readonly List<Book> books;

public MainViewModel( )
{
    this.books = new List<Book>
    {
        new Book
        {
            Name = "Book 1",
            Type = BookType.Fantasy
        },
        new Book
        {
            Name = "Book 2",
            Type = BookType.Fantasy
        },
        new Book
        {
            Name = "Book 3",
            Type = BookType.Fantasy
        },
        new Book
        {
            Name = "Book 4",
            Type = BookType.SciFi
        }
    };
}

public ICollectionView Books
{
    get
    {
        var source = CollectionViewSource.GetDefaultView( this.books );
        source.GroupDescriptions.Add( new PropertyGroupDescription( "Type" ) 
    );
    return source;
}

看法:

<TreeView ItemsSource="{Binding Books.Groups}">
  <TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Items}">
      <TextBlock Text="{Binding Name}" />
    </HierarchicalDataTemplate>
  </TreeView.ItemTemplate>
</TreeView>

據我了解,您需要幫助在TreeView使用ViewModel生成層次結構。 為此,您將使用HierarchicalDataTemplate 您的ViewModel將需要一個子項集合,您將使用簡單的將HierarchicalDataTemplateItemsSource屬性綁定到該集合。

看一下某人制作的這個簡單示例: TreeView and HierarchicalDataTemplate, Step-by-Step

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM