-1
我在我的第一頁上列出了列表。只要用戶雙擊listVIew中的一個項目,我就應該顯示一個GridView,他們可以在其中添加/更新/刪除項目。我的代碼工作,直到雙擊。我也看到了我的GridView上的正確綁定,並從數據庫中獲取了正確的項目數,但UI沒有顯示任何內容。每個單獨的項目都有一個40的綁定錯誤。請幫助!如何通過ViewModel將observableCollection綁定到DataGrid?
這是錯誤,當我窺探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); }
}
這正好解決QuestionCode錯誤,但ItemsSource時具有相同的綁定錯誤40 。 System.Windows.Data Error:40:BindingExpression path error:'CheckListQuestions'property not found on'object'''CheckListQuestionViewM odel'(HashCode = 54204373)'。 BindingExpression:路徑= CheckListQuestions; DataItem ='CheckListQuestionViewModel'(HashCode = 54204373);目標元素是'DataGrid'(Name ='gridQuestionList');目標屬性是'ItemsSource'(類型'IEnumerable') – user2832411
請參閱我的編輯.... – Clemens
感謝您的及時響應!將其改爲公共工作! – user2832411