簡體   English   中英

將可觀察的集合模型屬性綁定到網格中的下拉列

[英]Bind observable collection model property to dropdown column in grid

我正在使用syncfusion網格控件,但是我認為這個問題是通用的嗎?

我已將此網格綁定到客戶列表。 這些屬性(名稱,電子郵件,聯系方式等)在網格內顯示“確定”。

現在,我預計一個客戶可以擁有1個或多個地址(特別是如果它們是業務分支)。

因此,我還有一個帶有網格的下拉列類型來顯示潛在的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>

我的VM是...

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; }
}

虛擬機:

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路徑錯誤:在“對象”“ ApplicationViewModel”(HashCode = 59362130)'上找不到“ Addresses”屬性。 BindingExpression:Path =地址; DataItem ='ApplicationViewModel'(HashCode = 59362130); 目標元素是“ GridComboBoxColumn”(HashCode = 54875957); 目標屬性為“ ItemsSource”(類型為“ IEnumerable”)

雖然我了解該錯誤,但我不知道“修復”該錯誤。 如何使“子”項源與父項源“相關”?

您可以嘗試用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM