WPF绑定数据未加载到绑定控件 [英] WPF bound data not loading to bound controls

查看:70
本文介绍了WPF绑定数据未加载到绑定控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偏离了此处说明的方法(无法使用)。

I've deviated from the methods illustrated here (which I was unable to get to work).

绑定数据在我根据此问题实施了MVVM

我遵循了一个教程(此后我丢失了链接)为我的DataGrid创建数据绑定。

I followed a tutorial (which I've since lost the link to) to create a data binding for my DataGrid.

该过程涉及从我的模型创建数据源,这使我可以将DataGrid拖到我的窗口上。

The process involved creating a data source from my model which allowed me to drag a DataGrid onto my Window.

当我运行它时,DataGrid在窗口上呈现空白

The DataGrid renders empty on the window when I run it and I know for a fact there is data that needs to go into it.

这是代码:

<Page.Resources>
    <CollectionViewSource x:Key="campaignViewSource" d:DesignSource="{d:DesignInstance {x:Type Models:Campaign}, CreateList=True}"/>
</Page.Resources>

<DataGrid x:Name="campaignDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" Grid.Row="1" RowDetailsVisibilityMode="VisibleWhenSelected">
    <DataGrid.Columns>
        <DataGridTextColumn x:Name="clientColumn" Binding="{Binding Client}" Header="Client" Width="SizeToHeader"/>
        <DataGridTemplateColumn x:Name="dateSentColumn" Header="Date Sent" Width="SizeToHeader">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <DatePicker SelectedDate="{Binding DateSent, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTextColumn x:Name="idColumn" Binding="{Binding Id}" Header="#" Width="SizeToHeader"/>
        <DataGridTextColumn x:Name="nameColumn" Binding="{Binding Name}" Header="Name" Width="SizeToHeader"/>
        <DataGridTextColumn x:Name="emailAddressColumn" Binding="{Binding EmailAddress}" Header="Email Address" Width="SizeToHeader"/>
    </DataGrid.Columns>
</DataGrid>

根据我从中构建此教程的过程,我无需做任何其他工作即可完成此工作。那我错过了什么?

According to the tutorial I built this from, there's nothing more that I need to do to make this work. So what have I missed?

我应该如何进行?

推荐答案

尝试绑定将 ItemsSource 属性添加到 CollectionViewSource

Try to bind the ItemsSource property to your CollectionViewSource:

<DataGrid x:Name="campaignDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True"
          ItemsSource="{Binding Source={StaticResource campaignViewSource}}" Grid.Row="1" RowDetailsVisibilityMode="VisibleWhenSelected">

您还需要设置 Source 属性 CollectionViewSource 到某些源集合:

You also need to set the Source property of the CollectionViewSource to some source collection:

var cvs = this.Resources["campaignViewSource"] as CollectionViewSource;
cvs.Source = new List<YourDataObject> { new YourDataObject(), new YourDataObject() };

拖放方法实际上并不是做或学到东西的好方法。如果您想成为一名成功的WPF开发人员,则应该学习XAML和MVVM。

The drag-and-drop approach is really no good way of either doing or learning something. You should learn XAML and MVVM if you want to become a successful WPF developer.

这篇关于WPF绑定数据未加载到绑定控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆