【问题标题】:How to bind observableCollection to DataGrid via ViewModel?如何通过 ViewModel 将 observableCollection 绑定到 DataGrid?
【发布时间】: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


    【解决方案1】:

    替换

    ItemsSource="{Binding Source=CheckListQuestions}" 
    

    通过

    ItemsSource="{Binding Path=CheckListQuestions}" 
    

    或者只是

    ItemsSource="{Binding CheckListQuestions}" 
    

    使用Source=CheckListQuestions 时,ItemsSource 属性获取字符串“CheckListQuestions”作为值,即IEnumerable&lt;char&gt;。因此出现错误'QuestionCode' property not found on 'object' ''Char'


    还要确保CheckListQuestions 是公共属性,即

    public ObservableCollection<ChecklistQuestion> CheckListQuestions { get; set; }
    

    而不是

    ObservableCollection<ChecklistQuestion> CheckListQuestions { get; set; }
    

    【讨论】:

    • 修复了 QuestionCode 错误,但 itemsSource 具有相同的绑定错误 40. System.Windows.Data 错误:40:BindingExpression 路径错误:在 'object' ''CheckListQuestionViewModel' 上找不到 'CheckListQuestions' 属性(哈希码=54204373)'。 BindingExpression:Path=CheckListQuestions; DataItem='CheckListQuestionViewModel' (HashCode=54204373);目标元素是“DataGrid”(名称=“gridQuestionList”);目标属性是“ItemsSource”(类型“IEnumerable”)
    • 感谢您的及时回复!将其更改为公共工作!
    猜你喜欢
    • 2013-08-07
    • 2021-12-10
    • 2016-11-01
    • 2013-09-07
    • 2014-08-23
    • 2016-05-03
    • 1970-01-01
    • 2012-12-19
    相关资源
    最近更新 更多