【发布时间】:2017-12-28 21:43:10
【问题描述】:
我在第一页上列出了 VIew。一旦用户双击该 listVIew 中的一个项目,我应该显示一个 GridView,他们可以在其中添加/更新/删除项目。我的代码一直在工作,直到双击。我还在我的 GridView 上看到了正确的绑定,并且还从数据库中获得了正确的项目计数,但 UI 没有显示任何内容。每个单独项目的绑定错误为 40。请帮助!
这是我窥探 QuestionCode 时的错误:
System.Windows.Data Error: 40 : BindingExpression path error: 'QuestionCode' property not found on 'object' ''Char' (HashCode=4390979)'. BindingExpression:Path=QuestionCode; DataItem='Char' (HashCode=4390979); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
这是我的 xaml:
<DockPanel>
<DataGrid DockPanel.Dock="Top" x:Name="gridQuestionList" ItemsSource="{Binding Source=CheckListQuestions}" IsReadOnly="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Question Code" MinWidth="70">
<DataGridTemplateColumn.HeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
</DataGridTemplateColumn.HeaderStyle>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock VerticalAlignment="Center" Padding="3,0,8,0" Text="{Binding QuestionCode, ValidatesOnDataErrors=true, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox VerticalAlignment="Center" Text="{Binding QuestionCode, ValidatesOnDataErrors=true, UpdateSourceTrigger=PropertyChanged}" Foreground="Black" IsReadOnly="False"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
这是我的代码:
public partial class CheckListQuestionWindow : Window
{
private string _checklistCode = string.Empty;
private CheckListQuestionViewModel _viewModel;
public CheckListQuestionWindow()
{
InitializeComponent();
}
public CheckListQuestionWindow(string checkListCode) : this()
{
_checklistCode = checkListCode;
if (_viewModel == null)
_viewModel = new CheckListQuestionViewModel(_checklistCode);
DataContext = _viewModel;
}
}
这是我的视图模型:
ObservableCollection<ChecklistQuestion> CheckListQuestions { get; set; }
public CheckListQuestionViewModel(string code)
{
var list = ChecklistQuestionList.GetChecklistQuestionList(code);
CheckListQuestions = list;
//here i'm getting correct count of checklistquestions but there's no data
}
这是我的模特:
public string QuestionCode
{
get { return GetProperty(QuestionCodeProperty); }
set { SetProperty(QuestionCodeProperty, value); }
}
【问题讨论】:
标签: c# wpf datagridview binding observablecollection