本文介绍了如何将组合框中的选定项绑定到datagrid中的textblock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 hi i我在数据网格中使用了combobox而且组合框在cellediting模板中 i只想将组合框中的选定项目绑定到datgrid内的textblock 如果有人知道如何帮助我这里是我的xaml: < datagrid autogeneratecolumns = False virtualizingstackpanel.isvirtualizing = 错误 grid.row = 1 grid.columnspan = 7 name = attendancegrid 已删除 = #FFDCE8EB canuseraddrows = False > BorderBrush =ChocolateBorderThickness =5RowHeight =30IsSynchronizedWithCurrentItem =TrueHorizontalGridLinesBrush =#FFB74646IsReadOnly =False Foreground =BlackLoaded =attendancegrid_LoadedSelectionChanged =attendancegrid_SelectionChangedCurrentCellChanged =attendancegrid_CurrentCellChangedCellEditEnding =attendancegrid_CellEditEnding> < datagrid.columns > < datagridtextcolumn header = 小时 binding = {Binding time} fontsize = 14 fontweight = 粗体 isreadonly = True width = 100 / > < datagridtemplatecolumn header = 星期一 width = 100 > < datagridtemplatecolumn。 celleditingtemplate > < datatemplate > < stackpanel orientation = 水平 > < combobox x:name = 星期一 width = 50 selectedvalue = {Binding ProductName,Mode = TwoWay} issynchronizedwithcurrentitem = true 已加载 = monday_Loaded selectionchanged = monday_SelectionChanged xmlns:x = #unknown > < / combobox > < combobox x:名称 = staff width = 50 已加载 = staff_Loaded xmlns:x = #unknown > < / combobox > < / stackpanel > < / datatemplate > < / datagridtemplatecolumn。 celleditingtemplate > < datagridtemplatecolumn.celltemplate > < datatemplate > < stackpanel > < textblock x:name = mon text = {Binding ProductName} xmlns:x = #unknown > < / textblock > < / stackpanel > < / datatemplate > < / datagridtemplatecolumn.celltemplate > < / datagridtemplatecolumn > < / datagrid.columns > < datagrid。 columnheader style > < 样式 TargetType = DataGridColumnHeader > < setter property =FontSizevalue =15/ > < setter property =FontWeightvalue =Bold/> < setter property =Foregroundvalue =Chocolate/> < setter property =HorizontalContentAlignmentvalue =Center/> < / 风格 > < / datagrid.columnheader 样式 > < / datagrid > 解决方案 首先,我不明白你为什么要使用DataGrid?您可以使用ItemsControl创建更简单的解决方案。 其次,您正在尝试实现Master-Detail模式。 Combo-Box选择的值是Master,TextBlock是Details。 我写了一个简单的例子,它完全符合你在我的文章中想做的事情: 如何:使用主 - 细节模式使用WPF Hierarchical DataTemplate [ ^ ] 请查看。如果您有更多问题我会很乐意提供帮助。 最好的问候, Shai hi i am using combobox inside datagrid and the combobox is in cellediting templatei just want to bind the selected item in the combobox to textblock inside the datgridif any knows how to do it help me here is my xaml:<datagrid autogeneratecolumns="False" virtualizingstackpanel.isvirtualizing="False" grid.row="1" grid.columnspan="7" name="attendancegrid" removed="#FFDCE8EB" canuseraddrows="False"> BorderBrush="Chocolate" BorderThickness="5" RowHeight="30" IsSynchronizedWithCurrentItem="True" HorizontalGridLinesBrush="#FFB74646" IsReadOnly="False" Foreground="Black" Loaded="attendancegrid_Loaded" SelectionChanged="attendancegrid_SelectionChanged" CurrentCellChanged="attendancegrid_CurrentCellChanged" CellEditEnding="attendancegrid_CellEditEnding"> <datagrid.columns> <datagridtextcolumn header="Hours" binding="{Binding time}" fontsize="14" fontweight="Bold" isreadonly="True" width="100" /> <datagridtemplatecolumn header="Monday" width="100"> <datagridtemplatecolumn.celleditingtemplate> <datatemplate> <stackpanel orientation="Horizontal"> <combobox x:name="monday" width="50" selectedvalue="{Binding ProductName, Mode=TwoWay}" issynchronizedwithcurrentitem="true" loaded="monday_Loaded" selectionchanged="monday_SelectionChanged" xmlns:x="#unknown"></combobox> <combobox x:name="staff" width="50" loaded="staff_Loaded" xmlns:x="#unknown"></combobox> </stackpanel> </datatemplate> </datagridtemplatecolumn.celleditingtemplate> <datagridtemplatecolumn.celltemplate> <datatemplate> <stackpanel> <textblock x:name="mon" text="{Binding ProductName}" xmlns:x="#unknown"></textblock> </stackpanel> </datatemplate> </datagridtemplatecolumn.celltemplate> </datagridtemplatecolumn> </datagrid.columns> <datagrid.columnheaderstyle> <Style TargetType="DataGridColumnHeader"> <setter property="FontSize" value="15" /> <setter property="FontWeight" value="Bold" /> <setter property="Foreground" value="Chocolate" /> <setter property="HorizontalContentAlignment" value="Center" /> </Style> </datagrid.columnheaderstyle></datagrid> 解决方案 Hi,First, I don't understand why are you using DataGrid? You can create a much more simple solution using ItemsControl. Second, You are trying to implement a Master-Detail pattern. The Combo-Box selected value is the Master and the TextBlock is the Details. I've wrote a simple example that do exactly what you want to do in my article:How to: Use the Master-Detail Pattern with WPF Hierarchical DataTemplate[^]Please review it. If you have more questions I'll be happy to help.Best Regards,Shai 这篇关于如何将组合框中的选定项绑定到datagrid中的textblock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-14 15:21