将可观察的集合模型属性绑定到网格

时间:2017-10-30 12:00:31

标签: c# wpf viewmodel

我正在使用syncfusion网格控件,但我认为我的这个问题是通用的吗?

我将此网格绑定到客户列表。这些属性(名称,电子邮件,联系人号等)在网格内显示确定。

现在我预计客户端可以拥有一个或多个地址(特别是如果它们是业务分支)。

因此,我还有一个下拉列类型,其中包含网格以显示潜在的1+地址。

麻烦的是没有显示任何内容。

所以..

我的XAML是:

    <syncfusion:SfDataGrid 
          ItemsSource="{Binding Path=HeartBeat.ConciseCustomer}">
        <syncfusion:SfDataGrid.Columns>
            <syncfusion:GridTextColumn MappingName="Customer.FirstName" HeaderText="First Name" Width="150" />
            <syncfusion:GridTextColumn MappingName="Customer.LastName" HeaderText="Last Name" Width="150" />
            <syncfusion:GridComboBoxColumn MappingName="Address1"  DisplayMemberPath="Address1" ItemsSource="{Binding Addresses}" />
            <syncfusion:GridTextColumn MappingName="Customer.ContactNo1" HeaderText="Contact No" Width="130" />
            <syncfusion:GridTextColumn MappingName="Customer.EmailAddress1" HeaderText="Email Address"  Width="300"/>
        </syncfusion:SfDataGrid.Columns>
    </syncfusion:SfDataGrid>

我的虚拟机是......

private ObservableCollection<ConciseCustomer> _conciseCustomer;

public ObservableCollection<ConciseCustomer> ConciseCustomer
{
    get => _conciseCustomer;
    set
    {
        _conciseCustomer = value;
        RaisePropertyChanged("ConciseCustomer");
    }
}

我的模特是:

public class Address
{
    public Int64 AddressId { get; set; }
    public string AddressRef { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string Address3 { get; set; }
    public string Town { get; set; }
    public string PostCode { get; set; }
    public string County { get; set; }
    public string Country { get; set; }
    public string CustomerRef { get; set; }
    public bool Active { get; set; }
    public string AccountRef { get; set; }
    public DateTime? ServerTs { get; set; }
    public string ServerRef { get; set; }
}

public class Customer
{
    public Int64 CustomerId { get; set; }
    public string CustomerRef { get; set; }
    public string CustomerFriendlyRef { get; set; }
    public string Title { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public string ContactNo1 { get; set; }
    public string ContactNo2 { get; set; }
    public string ContactNo3 { get; set; }
    public string EmailAddress1 { get; set; }
    public string EmailAddress2 { get; set; }
    public DateTime Doe { get; set; }
    public bool Active { get; set; }
    public string AccountRef { get; set; }
    public DateTime? ServerTs { get; set; }
    public string ServerRef { get; set; }
}

VM:

public class ConciseCustomer : VMS
{
    private Customer _customer;
    private ObservableCollection< Address> _addresses;

    public Customer Customer
    {
        get => _customer;
        set
        {
            _customer = value;
            RaisePropertyChanged("Customer");
        }
    }

    public ObservableCollection<Address> Addresses
    {
        get => _addresses;
        set
        {
            _addresses = value;
            RaisePropertyChanged("Addresses");
        }
    }

}

public class ApplicationViewModel : VMS
{
    public ApplicationViewModel()
    {
        HeartBeat = new HeartBeat
        {
            BookingWizard = new BookingWizard(),
            LookUps = new LookUps()
        };
    }
    private HeartBeat _heartBeat;

    public HeartBeat HeartBeat
    {
        get => _heartBeat;
        set
        {
            _heartBeat = value;
            RaisePropertyChanged("HeartBeat");
        }
    }
}

输出窗口中的错误是?

System.Windows.Data错误:40:BindingExpression路径错误:&#39;地址&#39;在&#39; object&#39;上找不到的属性&#39;&#39; ApplicationViewModel&#39; (的HashCode = 59362130)&#39 ;. BindingExpression:路径=地址;的DataItem =&#39; ApplicationViewModel&#39; (的HashCode = 59362130);目标元素是&#39; GridComboBoxColumn&#39; (的HashCode = 54875957);目标属性是&#39; ItemsSource&#39; (键入&#39; IEnumerable&#39;)

虽然我理解了我不知道的错误并且修复了&#39;它。如何获得&#39; sub&#39; itemsource to&#39; related&#39;到父项目源?

1 个答案:

答案 0 :(得分:0)

您可以尝试将GridComboBoxColumn替换为GridTemplateColumn

<syncfusion:GridTemplateColumn MappingName="Address1" 
                               syncfusion:FocusManagerHelper.WantsKeyInput="True">
    <syncfusion:GridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Address1}" />
        </DataTemplate>
    </syncfusion:GridTemplateColumn.CellTemplate>
    <syncfusion:GridTemplateColumn.EditTemplate>
        <DataTemplate>
            <ComboBox SelectedItem="{Binding Address1}"  
                      ItemsSource="{Binding Addresses}"
                      DisplayMemberPath="Address1" />
        </DataTemplate>
    </syncfusion:GridTemplateColumn.EditTemplate>
</syncfusion:GridTemplateColumn>

但是从数据模型中仍然不清楚如何将Address连接到Customer