0
當IList綁定到數據網格時遇到問題。WPF DataGrid綁定
類結構,
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綁定到列,但它不會在網格中顯示當前行的銷售行名稱。但是,當我點擊下拉框時,下拉列表被填充。
請幫我在銷售行列中顯示銷售行名稱。