WPF DataGrid绑定

时间:2011-07-16 14:47:22

标签: wpf mvvm

当IList绑定到数据网格时,我遇到了一个问题。

班级结构是,

public class Customer
{
    public virtual int CustomerID { get; set; }
    public virtual string Name { get; set; }
    public virtual string AddressLine1 { get; set; }
    public virtual string AddressLine2 { get; set; }
    public virtual string AddressLine3 { get; set; }
    public virtual bool IsActive { get; set; }
    public virtual SalesLine SalesLine { get; set; }
    public virtual int Precedence { get; set; }
}

public class SalesLine
{
    public virtual int SalesLineID { get; set; }
    public virtual string Name { get; set; }
    public virtual SalesPerson SalesPerson { get; set; }
    public virtual IList<Customer> Customers { get; set; }
}

我创建了一个新的Customer类IList。

IList<Customer> customerList = new IList<Customer>();

然后填充“customerList”。 由于客户拥有SalesLine,因此“salesLines”IList将填充数据库中的所有销售线。

现在使用以下XAML,我将把customerList绑定到datagrid,

<DataGrid AutoGenerateColumns="False" Grid.Column="1" Grid.Row="1" HorizontalAlignment="Stretch"  
                  Name="grdCustomers" VerticalAlignment="Stretch" Grid.ColumnSpan="2" 
                  ItemsSource="{Binding CustomerList}" CanUserAddRows="True">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="250" />
                <DataGridTextColumn Header="Address Street" Binding="{Binding AddressLine1}" Width="150" />
                <DataGridTextColumn Header="Address Town" Binding="{Binding AddressLine2}" Width="150" />
                <DataGridTextColumn Header="Address Area" Binding="{Binding AddressLine3}" Width="150" />
                <DataGridCheckBoxColumn Header="Is Active" Binding="{Binding IsActive}" Width="75" />
                <DataGridComboBoxColumn Header="Sales Line"  Width="150"
                        SelectedItemBinding="{Binding  SalesLine}"
                        SelectedValuePath="SalesLineID"
                        DisplayMemberPath="Name">
                    <DataGridComboBoxColumn.ElementStyle>
                        <Style TargetType="ComboBox">
                            <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.SalesLineList}"/>
                        </Style>
                    </DataGridComboBoxColumn.ElementStyle>
                    <DataGridComboBoxColumn.EditingElementStyle>
                        <Style TargetType="ComboBox">
                            <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.SalesLineList}"/>
                        </Style>
                    </DataGridComboBoxColumn.EditingElementStyle>
                </DataGridComboBoxColumn>
            </DataGrid.Columns>
        </DataGrid>

现在它正确显示除销售线列以外的所有列。

即使我已将SalesLine绑定到列,它也不会在网格中显示当前行的销售线名称。但是,当我点击下拉框时,会填充下拉列表。

请帮我在销售线列上显示销售线名称。

1 个答案:

答案 0 :(得分:1)

这可能是因为Customer.SalesLine属性未指向SalesLineList

中的对象

您需要覆盖ToString()类中的SalesLine方法,以便在ID匹配时使两个对象相同。默认情况下,只有在memoroy中引用完全相同的对象时才会认为它们相同。