2014-01-06 41 views
2

我已經發布了一個前面的問題,但沒有什麼幫助,所以我試着開始編碼並查找一些更多的我自己的,我卡在一些代碼。我試圖按照MVVM創建文本框的問題?

我創建了一個名爲Standard類,看起來像這樣:

namespace MVVModel 
{ 
    public class Standard 
    { 

     string _title; 
     string _question; 



     public string Title 
     { 
      get { return _title; } 
      set { _title = value; } 
     } 

     public string Question 
     { 
      get { return _question; } 
      set { _question = value; } 
     } 
    } 
} 

然後創建ViewModel類,看起來像這樣:

namespace MVVModel 
{ 
    class ViewModel 
    { 

     ObservableCollection<Standard> _title = new ObservableCollection<Standard>(); 
     ObservableCollection<Standard> _question = new ObservableCollection<Standard>(); 

    public ViewModel() 
    { 

    } 

    public ObservableCollection<Standard> Title 
    { 
      get 
      { 
       return _title; 
      } 
      set 
      { 
       _title = value; 
      } 
    } 

    public ObservableCollection<Standard> Question 
    { 
      get 
      { 
       return _question; 
      } 
      set 
      { 
       _question = value; 
      } 
    } 
    } 
} 

這是我的XAML:

<Grid> 

    <Button x:Name="btnTitle" Content="Title" HorizontalAlignment="Left" Margin="691,22,0,0" VerticalAlignment="Top" Width="75"/> 
    <Button x:Name="btnQuestion" Content="Question" HorizontalAlignment="Left" Margin="797,22,0,0" VerticalAlignment="Top" Width="75" Command="{Binding AddTitle}"/> 

     <ItemsControl ItemsSource="{Binding Question}" Margin="0,86,0,0"> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <TextBox /> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 

</Grid> 

我只是想動態地創建一個文本框,但沒有顯示任何幫助?

+1

實施,必須先接受以前的答案,如果它幫你。 –

+1

列表中有多少個問題?如果你沒有插入任何內容,將不會有文本框。 – nvoigt

+0

您需要在您的類上實現['INotifyPropertyChanged'接口](http://msdn.microsoft.com/zh-cn/library/system.componentmodel.inotifypropertychanged.aspx)。但是,你真的不應該添加重複的問題......相反,你應該編輯你的原始問題。 – Sheridan

回答

1

我在前面的回答中提到有關實施INotifyPropertyChanged。

爲什麼您再次需要viewModel中的問題和標題集合,這已經存在於Standard類中。

您需要在您的主ViewModel中使用Standard類的集合。多數民衆贊成我從你的問題得到,如果我已經正確理解。

這裏是INotifyPropertyChanged的

public class Standard : INotifyPropertyChanged 
{ 

    public event PropertyChangedEventHandler PropertyChanged; 

    // Create the OnPropertyChanged method to raise the event 
     protected void NotifyOfPropertyChanged(string name) 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 
      if (handler != null) 
      { 
       handler(this, new PropertyChangedEventArgs(name)); 
      } 
     } 

     protected void NotifyOfPropertyChanged<TProperty>(Expression<Func<TProperty> property) 
     { 
      NotifyOfPropertyChanged(property.GetMemberInfo().Name); 
     } 

    string _title; 
    ObservableCollection<string> _questions;   

    public string Title 
    { 
     get { return _title; } 
     set { 
      _title = value; 
      NotifyOfPropertyChanged(()=>Title); 
     } 
    } 

    public ObservableCollection<string> Questions 
    { 
     get { return _questions; } 
     set { 
      _questions = value; 
      NotifyOfPropertyChanged(()=>Questions); 
     } 
    } 
} 
+0

我已經瀏覽了您的上一個答案。它說'NofityPropertyChanged'需要一個命名空間? XAML也說'Text'已經定義好了嗎? – user3157821

+0

你需要實現INotifyPropertyChanged接口。好吧,讓我做 –

+0

感謝您的編輯,'保護無效NotifyOfPropertyChanged(表達式>財產)'TProperty'有一個錯誤,說'它無法找到'。在同樣的方法'property.GetMemberInfo'' GetMemberInfo'說'System.Linq.Expressions不包含GetMemberInfo的定義'最後,lambda表達式不會轉換爲字符串。 – user3157821

0

您必須按照幾個步驟完成此任務。

  1. 首先,你需要的Standard集合綁定到Grid,代替Question

  2. 其次,您需要將前一個類的屬性綁定到文本框。

例:

<DataTemplate> 
    <TextBox Text="{Binding Question"} /> 
</DataTemplate> 

編輯:

我想提的這篇文章,以幫助您:

http://www.codeproject.com/Articles/165368/WPF-MVVM-Quick-Start-Tutorial

+0

首先,當我將'Standard'綁定到'Grid'時,我用'Grid Command =「{}」>和'「做什麼屬性不起作用。其次,我得到一個錯誤你的代碼片段「WPF項目不支持問題」 – user3157821

+0

你不需要命令。我附上了快速教程,以幫助您。我認爲你最好遵循一個完整的教程,並取得一些簡單問題/答案的經驗。 – gustavodidomenico

+0

我瀏覽了一下教程並下載了它,看看它是如何工作的,現在我明白了一點。在那裏'ViewModel'它會和我不一樣,因爲我只是想創建一個'Textbox',我會在這裏添加什麼? – user3157821