[英]Bind observable collection model property to dropdown column in grid
I am using the syncfusion grid control but I assume this question of mine is generic? 我正在使用syncfusion网格控件,但是我认为这个问题是通用的吗?
I have this grid bound to a customer list. 我已将此网格绑定到客户列表。 These properties (name, email, contact no etc) display Ok inside the grid. 这些属性(名称,电子邮件,联系方式等)在网格内显示“确定”。
Now I am anticipating that a client can have 1 or more addresses (especially if they are business branches). 现在,我预计一个客户可以拥有1个或多个地址(特别是如果它们是业务分支)。
So, i also have a dropdown column type withing the grid to show thepotential 1+ addresses. 因此,我还有一个带有网格的下拉列类型来显示潜在的1个以上的地址。
Trouble is this is not showing anything. 问题是这没有显示任何东西。
So.. 所以..
My XAML is: 我的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>
My VM is... 我的VM是...
private ObservableCollection<ConciseCustomer> _conciseCustomer;
public ObservableCollection<ConciseCustomer> ConciseCustomer
{
get => _conciseCustomer;
set
{
_conciseCustomer = value;
RaisePropertyChanged("ConciseCustomer");
}
}
My models are: 我的模型是:
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");
}
}
}
The error in the output window is? 输出窗口中的错误是?
System.Windows.Data Error: 40 : BindingExpression path error: 'Addresses' property not found on 'object' ''ApplicationViewModel' (HashCode=59362130)'. System.Windows.Data错误:40:BindingExpression路径错误:在“对象”“ ApplicationViewModel”(HashCode = 59362130)'上找不到“ Addresses”属性。 BindingExpression:Path=Addresses; BindingExpression:Path =地址; DataItem='ApplicationViewModel' (HashCode=59362130); DataItem ='ApplicationViewModel'(HashCode = 59362130); target element is 'GridComboBoxColumn' (HashCode=54875957); 目标元素是“ GridComboBoxColumn”(HashCode = 54875957); target property is 'ItemsSource' (type 'IEnumerable') 目标属性为“ ItemsSource”(类型为“ IEnumerable”)
Whilst I understand the error I do not know to 'fix' it. 虽然我了解该错误,但我不知道“修复”该错误。 How do I get the 'sub' itemsource to 'relate' to the parent itemsource? 如何使“子”项源与父项源“相关”?
You could try to replace the GridComboBoxColumn
with a GridTemplateColumn
: 您可以尝试用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>
But it is still unclear from from data model how you are supposed to connect an Address
to a Customer
. 但是从数据模型中仍然不清楚如何将Address
连接到Customer
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.