gpt4 book ai didi

c# - 两个表的 Wpf DatagridComboBoxColumn 绑定(bind)?

转载 作者:太空宇宙 更新时间:2023-11-03 13:06:45 26 4
gpt4 key购买 nike

我必须上课 Books and Publishing Houses:

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。我必须以某种方式为两个数据源绑定(bind)“DataGridComboBoxColumn”吗?

最佳答案

您的 Book 类应该有一个 Property,其中包含所有可用 PublishingHousesphId 列表,如下所示:

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>

编辑

If i do so, every object of Book will contain identical collections of avaliable PublishingHouses. That's very unefficient by memory. Is it any other solution?

为了防止数据冗余,您可以在父类 中定义PhIdList(例如,如果您使用的是 mvvm,则为书单容器 View 模型)并获取 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>

关于c# - 两个表的 Wpf DatagridComboBoxColumn 绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30555954/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com