因此,我有一個WPF c#項目,並在一個usercontrol中,我有一個列表,它通過DataTemplateSelector設置集合的顯示。那些包含一些簡單字符串和整數的數據綁定對象,以及一個名爲Answers的可觀察集合。我用所需的PropertyChangedEventHandler和函數在對象內部實現了INotifyPropertyChanged。從DataTemplate中的DataGrid綁定到字符串ObservableCollection
因此,繼承人生活在被稱爲顯示的listItem
<DataGrid x:Name="AnswersListBox" HorizontalAlignment="Stretch" Grid.Column="3" Grid.Row="3" Grid.ColumnSpan="2" ItemsSource="{Binding Answers}"
AutoGenerateColumns="False" Margin="10,10,10,10" CanUserResizeRows="False" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=DataContext,
RelativeSource={RelativeSource Self}, Mode=TwoWay}" Header="Possible Answers"
Width="130" IsReadOnly="False" CanUserResize="False" CanUserSort="False" CanUserReorder="False" MaxWidth="130"/>
</DataGrid.Columns>
</DataGrid>
圖像顯示綁定對象
我的問題是工作數據的datatempate XAML中的片段,顯示答案的數據網格(目前有'重複一個單詞')不會更新... 我嘗試了很多與此編輯可能變化tview與文本框列表框,但是這個數據網格是最接近伊夫得到,但它不會讓一個編輯的值observerablecollection內
編輯以添加更多的代碼
繼承人的ObserverableCollection
namespace CRUDeBuilder.chunkProtos
{
public class MultiChoice : Question
{
/// <summary>
/// this is a type of question that holds a statement and several answers
/// </summary>
private ObservableCollection<string> answers = new ObservableCollection<string>();
private int correctAnswer = 0;
private bool randomise = false;
public MultiChoice()
{
base.QuestionType = "multiChoice";
}
public ObservableCollection<string> Answers
{
get
{
return answers;
}
set
{
answers = value;
base.NotifyPropertyChanged("Answers");
}
}
// ive just chopped off bits here
INotifyPropertyChanged的的實施是在Multichoice基類:問題是
public abstract class Question : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
背後的代碼添加項目答案的ObservableCollection
private void addAnswerBtn_Click(object sender, RoutedEventArgs e)
{
Button button = sender as Button;
Question question = button.DataContext as Question;
//Console.WriteLine("clicking add answer button");
if (question is MultiChoice)
{
Console.WriteLine("is a multichoice ");
((MultiChoice)question).addAnswer("a word");
//((MultiChoice)question).Answers.Add("a word");
// both of these add, but I added the custom version to check
}
else
{
Console.WriteLine("is NOT a multichoice");
}
}
我看不出有什麼伊夫錯過讓這些編輯答案被設置爲數據
向我們展示你的ObservableCollection定義和用於更新代碼它。我們無法提供這麼少的信息。 – Pikoh