0
我有一個數據網格在我的WPF應用程序窗口,我必須綁定到一個可觀察的集合。我必須向用戶提供選項才能編輯,刪除,添加和編輯此數據網格中的項目。編輯,刪除,添加項目到數據網格很容易
我在下面做了datagrid的綁定,但想問我該如何讓用戶輕鬆添加,編輯/更新,從數據網格中刪除項目。
這裏是我當前的代碼:
的.xaml
<DataGrid x:Name="dgrQuestions" AutoGenerateColumns="False" Height="272" HorizontalAlignment="Left" Margin="42,73,0,0" VerticalAlignment="Top" Width="669" ItemsSource="{Binding QuestionsList}">
<DataGrid.Columns>
<DataGridTextColumn Header="FirstQues" Binding="{Binding Question}"/>
<DataGridTextColumn Header="SecondQues" Binding="{Binding Answer}"/>
<DataGridTextColumn Header="ThirdQues" Binding="{Binding Hint}"/>
</DataGrid.Columns>
</DataGrid>
.xaml.cs代碼
using Quest; //class having the format of the question
public class Master
{
private ObservableCollection<Quest> questList;
public event PropertyChangedEventHandler PropertyChanged;
public ObservableCollection<Quest> QuestionList
{
get { return questList; }
set
{
questList = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("QuestionsList"));
}
}
//Add, Edit, Remove, Reset methods required
}
的.cs代碼(僅性質這裏提到)的爲其創建對象的類:
public string Question
{
get { return _question; }
set{ _question = value; OnPropertyChanged("Question"); }
}
public string Answer
{ get { return _answer; }
set {_answer = value; OnPropertyChanged("Answer"); }
}
public string Hint
{
get { return _hint; }
set { _hint = value; OnPropertyChanged("Hint"); }
}