DataGridComboBoxColumn 显示外键 Table - WPF
DataGridComboBoxColumn show Foreign Key Table - WPF
我有一个 SQL 数据库,其中包含 table 个 Jar 和 JarType。我需要在 DataGrid 中显示数据。我还必须能够编辑行。我认为 DataGridComboBoxColumn 非常适合这项工作。我如何用 JarTypes.Type 填充 ComboBox 并将 TypeId 放回 Jars.TypeId。 table 存储在 DataTables JarsDt 和 TypeDt
中
罐子 (JarsDt)
- JarId (PK)
- 金额
- TypeId (FK)
JarTypes (TypeDt)
- TypeId (PK)
- 类型
XAML
<DataGrid x:Name="JarDataGrid"
AutoGenerateColumns="False"
IsReadOnly="False"
HorizontalAlignment="Stretch"
Margin="10,35,2,36.8">
<DataGrid.Columns>
<DataGridTextColumn Header="#"
IsReadOnly="True"
Binding="{Binding JarId}" />
<DataGridTextColumn Header="Mengde"
Binding="{Binding Amount}" />
<DataGridComboBoxColumn x:Name="combo"
Header="Glass"
SelectedValueBinding="{Binding TypeId}"
DisplayMemberPath="{Binding Type}"/>
<DataGridTextColumn Header="Mengde levert"
IsReadOnly="True"
Binding="{Binding AmountDelivered}" />
将 DataGrid
的 ItemsSource
属性 设置为 Jar 列表,将 ComboBox
的 ItemsSource
属性 设置为一个JarType 列表。
您可以在 XAML 中使用 CollectionViewSource
或通过直接设置 JarDataGrid.ItemsSource = ListOfJars
.
在代码中执行此操作
您需要设置DataGridComboBoxColumn
的SelectedValuePath
属性。
另外DisplayMemberPath
是一个字符串属性,所以不使用绑定。
你的情况是:
<DataGridComboBoxColumn x:Name="combo"
Header="Glass"
DataContext="{StaticResource jarTypesViewSource}"
ItemsSource="{Binding}"
SelectedValueBinding="{Binding TypeId}"
SelectedValuePath="TypeId"
DisplayMemberPath="Type"/>
我有一个 SQL 数据库,其中包含 table 个 Jar 和 JarType。我需要在 DataGrid 中显示数据。我还必须能够编辑行。我认为 DataGridComboBoxColumn 非常适合这项工作。我如何用 JarTypes.Type 填充 ComboBox 并将 TypeId 放回 Jars.TypeId。 table 存储在 DataTables JarsDt 和 TypeDt
中罐子 (JarsDt)
- JarId (PK)
- 金额
- TypeId (FK)
JarTypes (TypeDt)
- TypeId (PK)
- 类型
XAML
<DataGrid x:Name="JarDataGrid"
AutoGenerateColumns="False"
IsReadOnly="False"
HorizontalAlignment="Stretch"
Margin="10,35,2,36.8">
<DataGrid.Columns>
<DataGridTextColumn Header="#"
IsReadOnly="True"
Binding="{Binding JarId}" />
<DataGridTextColumn Header="Mengde"
Binding="{Binding Amount}" />
<DataGridComboBoxColumn x:Name="combo"
Header="Glass"
SelectedValueBinding="{Binding TypeId}"
DisplayMemberPath="{Binding Type}"/>
<DataGridTextColumn Header="Mengde levert"
IsReadOnly="True"
Binding="{Binding AmountDelivered}" />
将 DataGrid
的 ItemsSource
属性 设置为 Jar 列表,将 ComboBox
的 ItemsSource
属性 设置为一个JarType 列表。
您可以在 XAML 中使用 CollectionViewSource
或通过直接设置 JarDataGrid.ItemsSource = ListOfJars
.
您需要设置DataGridComboBoxColumn
的SelectedValuePath
属性。
另外DisplayMemberPath
是一个字符串属性,所以不使用绑定。
你的情况是:
<DataGridComboBoxColumn x:Name="combo"
Header="Glass"
DataContext="{StaticResource jarTypesViewSource}"
ItemsSource="{Binding}"
SelectedValueBinding="{Binding TypeId}"
SelectedValuePath="TypeId"
DisplayMemberPath="Type"/>