您好我正在尝试在代码内的 DataGrid 中动态绑定 ComboBox。我看到了一些与此相关的答案,但没有一个有帮助。一般的意见是使用 DataTempleteColumn 但这也没有结果。这是我的代码
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Name="ButFill" Content="Fill Grid" HorizontalAlignment="Left" Height="22" Margin="373,65,0,0" VerticalAlignment="Top" Width="62"/>
<DataGrid x:Name="DaGrid" HorizontalAlignment="Left" Height="134" Margin="25,38,0,0" VerticalAlignment="Top" Width="289" ItemsSource="{Binding}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="text" Binding="{Binding Path=col1}"/>
<DataGridComboBoxColumn Header="combobox" Width="105" ItemsSource="{Binding Path=fill_items}"/>
<DataGridTemplateColumn Header="template combo" Width="105">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox Name="TempCombo" ItemsSource="{Binding Path=fill_items}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
后面的代码是
Imports System.Collections.ObjectModel
Class MainWindow
Public Property Table As New ObservableCollection(Of Test.dataset)()
Public Property fill_items As New ObservableCollection(Of String)
Private Sub ButFill_Click(sender As Object, e As RoutedEventArgs) Handles ButFill.Click
Dim temp As New Test.dataset()
Dim cb As New ComboBox
fill_items.Add("ItemNo1")
fill_items.Add("ItemNo2")
cb.ItemsSource = fill_items
temp.col1 = " Hello"
temp.col2 = cb
temp.col3 = cb
Table.Add(temp)
DaGrid.DataContext = Table
End Sub
End Class
Public Class dataset
Public Property col1 As String
Public Property col2 As ComboBox
Public Property col3 As ComboBox
End Class
我看到的问题是:
1) DataGridComboBox 列在进入编辑模式之前不会显示它。
2)两个组合框都是空的,但集合“表”似乎有一个 combobox.count 2。
我做错什么了吗?有人可以向我展示一个完整的绑定 ComboBox 示例吗?