我必须上书和出版社:
public partial class Books
{
public Books()
{
this.Authors = new ObservableCollection<Authors>();
}
public int bID { get; set; }
public string Title { get; set; }
public int phID { get; set; }
public Nullable<short> PubYear { get; set; }
public Nullable<short> Edition { get; set; }
public virtual PublishingHouse PublishingHouse { get; set; }
public virtual ObservableCollection<Authors> Authors { get; set; }
}
public partial class PublishingHouse
{
public PublishingHouse()
{
this.Books = new ObservableCollection<Books>();
}
public int phID { get; set; }
public string Title { get; set; }
public string City { get; set; }
public virtual ObservableCollection<Books> Books { get; set; }
}
所以我有Datagrid,它向我展示了我所有的书:
<Window.Resources>
<CollectionViewSource x:Key="booksViewSource" d:DesignSource="{d:DesignInstance {x:Type local:Books}, CreateList=True}"/>
<CollectionViewSource x:Key="booksAuthorsViewSource" Source="{Binding Authors, Source={StaticResource booksViewSource}}"/>
<CollectionViewSource x:Key="authorsViewSource" d:DesignSource="{d:DesignInstance {x:Type local:Authors}, CreateList=True}"/>
<CollectionViewSource x:Key="publishingHouseViewSource" d:DesignSource="{d:DesignInstance {x:Type local:PublishingHouse}, CreateList=True}"/>
</Window.Resources>
<Grid DataContext="{StaticResource booksViewSource}" Margin="0,0,2,0">
<DataGrid ItemsSource="{Binding}">
<DataGrid.Columns>
<DataGridTextColumn Header="b ID" Binding="{Binding bID}"/>
<DataGridTextColumn Header="Title" Binding="{Binding Title}"/>
<DataGridTextColumn Header="#" Binding="{Binding Edition}"/>
<DataGridTextColumn Header="Year" Binding="{Binding PubYear}"/>
<DataGridComboBoxColumn x:Name="phIDComboBoxColumn" SelectedItemBinding="{Binding phID}" />
</DataGrid.Columns>
</DataGrid>
我想要的是设置要从书中获取的
phIDComboBoxColumn
的默认值,但是当我要对其进行编辑时,我想查看所有可用的phId
的PublishingHouses
列表。我是否必须以某种方式为两个数据源绑定“ DataGridComboBoxColumn”? 最佳答案
您的Book
类应具有一个Property
,其中包含所有可用phId's
的PublishingHouses
列表,如下所示:
public ObservableCollection<int> PhIdList {get; set;}
那么您的
DataGridComboBoxColumn
会这样变化:<DataGridComboBoxColumn Header="PhIDs" SelectedItemBinding="{Binding phID}">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding PhIdList}"/>
<Setter Property="IsReadOnly" Value="True"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding PhIdList}"/>
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
编辑
如果我这样做,则Book的每个对象都将包含可用的PublishingHouses的相同集合。内存效率很低。还有其他解决方案吗?
为了防止数据冗余,您可以在父类中定义
PhIdList
(例如,如果使用的是mvvm,则为书单容器视图模型),并获取如下的ComboBox
数据: <DataGridComboBoxColumn
SelectedItemBinding="{Binding phID}" DisplayMemberPath="" >
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="ItemsSource" Value="{Binding Path=DataContext.BooksContainerVM.PhIdList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="ItemsSource" Value="{Binding Path=DataContext.BooksContainerVM.PhIdList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>