WPF将Datagrid嵌套在datagrid详细信息中{binding} [英] WPF nested Datagrid in datagrid details {binding}
问题描述
我正在尝试将组合框绑定到我的VM中的列表,
该组合框位于一个数据网格中,此数据网格已绑定到另一个数据网格.
我应该如何设置绑定?
现在设置的所有绑定都可以正常工作,例如组合框上的绑定.
我似乎找不到在主窗口中设置的数据上下文...
到目前为止,这就是我所拥有的(当然没有用.)
我现在在xaml中拥有的内容:
I''m trying to bind a combobox to a list in my VM,
the combobox is located in a datagrid, this datagrid is binded to another datagrid.
How should I set up binding?
All binding that is set up now works just fine, exept for the binding on the combobox.
I can''t seem to find my datacontext that is set in the mainwindow...
This is what i have so far (It does''nt work ofcourse..)
What i have now, in xaml:
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<Grid>
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding ListBarcode}">
<DataGrid.Columns >
<DataGridTextColumn Binding="{Binding Path=Name,Mode=TwoWay,
ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged,
ValidatesOnExceptions=True}" Header="BarcodeName" Width="Auto" />
<DataGridTextColumn Binding="{Binding Path=Number,Mode=TwoWay,
ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged,
ValidatesOnExceptions=True}" Header="BarcodeNumber" Width="Auto" />
<DataGridComboBoxColumn Header="Brand" Width="1*" MaxWidth="100"
ItemsSource="{Binding Path=DataContext.ListBrands,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window},
AncestorLevel=1}}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
提前谢谢!
答案: WPF将Datagrid嵌套在datagrid详细信息中{binding} [ ^ ]
Thanks in advance!
Answer: WPF nested Datagrid in datagrid details {binding}[^]
推荐答案
< DataGridComboBoxColumn标头="Merk" Width ="1 *" MaxWidth ="100" ItemsSource ="{Binding RelativeSource = {RelativeSource FindAncestor,AncestorType = Window,AncestorLevel = 1},Path = DataContext. >}"/>
<DataGridComboBoxColumn Header="Merk" Width="1*" MaxWidth="100" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window, AncestorLevel=1}, Path=DataContext.Merkenlijst}" />
我在您的XAML中看到一个拼写错误. Merkenlijst
是正确的还是应该是Merkenlist
?
I see a typo mistake in the your XAML. Merkenlijst
is this correct or is it suppose to Merkenlist
?
因为DataGridComboBoxColumn不继承自FrameworkElement,所以它不在可视化树中呈现,因此您不能使用相对绑定.
数据网格中的列没有数据上下文,因为它们从未添加到可视化树中.绘制网格后,单元格便具有数据上下文,您可以使用常规绑定(不是静态资源...)在其中设置组合框项目源.
XML
Because DataGridComboBoxColumn doesn''t inherit from FrameworkElement it is not rendered in the visual tree, so you can''t use relative binding.
The columns in the datagrid dont have a datacontext, as they are never added to the visual tree. Once the grid is drawn the cells have a data context and you can set the combo boxes items source in them using normal bindings (not static resources...)
XML
<DataGridComboBoxColumn Header="Brand" Width="1*" MaxWidth="100" TextBinding="{Binding Path=Merk}">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="SelectedItem" Value="{Binding Path=Merk}"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Path=Merklijst}" />
<Setter Property="SelectedItem" Value="{Binding Path=Merk}"/>
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
一些带有一些代码和说明的链接:
http://wpf.codeplex.com/Thread/View.aspx?ThreadId=46627 [ ^ ]
http://stackoverflow.com/questions/1633800/wpf-datagrid-datagridcomboxbox-itemssource-binding-to-a-collection-of-collection [ http://stackoverflow.com/questions/1903057/wpf-datagridcomboboxcolumn [ http://blogs.msdn.com/vinsibal/archive/2008/08/14/wpf-datagrid-dissecting-the-visual-layout.aspx [
Some links with some code and explenations:
http://wpf.codeplex.com/Thread/View.aspx?ThreadId=46627[^]
http://stackoverflow.com/questions/1633800/wpf-datagrid-datagridcomboxbox-itemssource-binding-to-a-collection-of-collection[^]
http://stackoverflow.com/questions/1903057/wpf-datagridcomboboxcolumn[^]
http://blogs.msdn.com/vinsibal/archive/2008/08/14/wpf-datagrid-dissecting-the-visual-layout.aspx[^]
这篇关于WPF将Datagrid嵌套在datagrid详细信息中{binding}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!