如何在DevExpression TreeListColumn上绑定ViewModel?

时间:2012-10-10 10:47:32

标签: .net wpf xaml mvvm devexpress

我想将MyCustomers绑定到TreeListControl TreeListColumn FieldName =“Id”Binding =“如何绑定CustomerId”我该怎么做?

<dxg:TreeListControl Name="MyControl" ItemsSource="{Binding Path=MyCustomers}" 
     Height="296" Width="750" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Left">
    <dxg:TreeListControl.Columns>
        <dxg:TreeListColumn FieldName="Id"   Binding="How to Bind CustomerID ?????"    />
        <dxg:TreeListColumn FieldName="CustomerGroup" Binding="How to Bind CustomerGroup ?????" />
    </dxg:TreeListControl.Columns>
    <dxg:TreeListControl.View>
        <dxg:TreeListView AutoWidth="True" KeyFieldName="Id" ParentFieldName="ParentId" NavigationStyle="Row" />
    </dxg:TreeListControl.View>
</dxg:TreeListControl>

视图模型:

public class CustomerViewModel : BaseViewModel
{
    public IList<Model.App.Customer> MyCustomers
    {
        get;
        private set;
    }

    public CustomerViewModel()
    {
        CustomerDetail = new CustomerDetail();
        this.MyCustomers = CustomerRespiratory.SelectMyCustomers();
    }
}

型号:

public class Customer 
{
  public int CustomerID { get; set;}
  public int CustomerGroup { get; set;}
}

1 个答案:

答案 0 :(得分:1)

您应该只将CustomerID指定为Key Field,将CustomerGroup指定为Parent Field。在"Binding to Self-Referential Data Structure"文章中已经清楚地描述了它:

<dxg:TreeListControl ItemsSource="{Binding Path=MyCustomers}">
    <dxg:TreeListControl.Columns>
        <dxg:TreeListColumn FieldName="CustomerId"/>
        <dxg:TreeListColumn FieldName="CustomerGroup"/>
    </dxg:TreeListControl.Columns>
    <dxg:TreeListControl.View>
        <dxg:TreeListView KeyFieldName="CustomerId" ParentFieldName="CustomerGroup" 
                AutoWidth="True" NavigationStyle="Row" />
    </dxg:TreeListControl.View>
</dxg:TreeListControl>

您可以在"TreeListView Data Binding"文章中阅读更多内容。

另见:
DXTreeList Getting Started - Lesson 1 - Binding to Data