您好我正在尝试在代码中的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都是空的,但Collections“Table”似乎有一个2的combobox.count。
我做错了吗? 有人能告诉我一个绑定ComboBox的正确完整示例吗?
答案 0 :(得分:0)
如果要在窗口的DataContext中的组合框中显示fill_items,则需要更新comboboxcolumn绑定以使用BindingProxy
,因为DataGrid列不在datagrid的Visualtree中。 / p>
<Window.Resources>
<local:MyBindingProxy x:Key="myproxy" BindingData="{Binding}" />
</Window.Resources>
你必须将Mainwindow的DataContext设置为自己,如构造函数中的DataContext =this;
然后你需要为MyBindingProxy编写类:
public class MyBindingProxy : Freezable
{
public static readonly DependencyProperty BindingDataProperty =
DependencyProperty.Register("BindingData", typeof(object),
typeof(MyBindingProxy ), new UIPropertyMetadata(null));
protected override Freezable CreateInstanceCore()
{
return new MyBindingProxy();
}
public object BindingData
{
get { return (object)GetValue(BindingDataProperty ); }
set { SetValue(BindingDataProperty , value); }
}
}
然后您可以将绑定更新为:
<DataGridComboBoxColumn Header="combobox" Width="105" ItemsSource="{Binding BindingData.fill_items, Source={StaticResource myproxy}}"/>
答案 1 :(得分:0)
fill_items
需要是dataset
类中的公共属性,因为
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox Name="TempCombo" ItemsSource="{Binding Path=fill_items}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
引用绑定集合的每个项目。在您的情况下,它是dataset
项。
你不能只是简单地使用RelativeSource或ElementName来查找DataGrid并绑定到它,因为DataGridColumn
不会被添加到可视树中,因此这些绑定不起作用。