【发布时间】:2016-10-13 13:34:34
【问题描述】:
我要做的是将字符串从我的 DataGrid 中的特定列中取出,并在 Combobox.Text 属性中显示该字符串。我正在使用此代码:
var dataString = ((DataRowView)dgvMain.SelectedItem).Row["Column"].ToString();
我已经为此设置了一个断点以查看它在拉什么,它是作为项目包含在 Combobox 中的正确字符串,但是每当我尝试通过 Combobox.Text 设置它时,Combobox 都是空的。但是,我已将我的 Combobox 设置为 isEditable = True 和 ReadOnly = True 并且此方法有效,但是由于 Combobox 已绑定到我的 DataTable,因此在集合中选择一个项目将显示 System.Data.DataRowView。我也会将 XAML 添加到我的 Combobox 中:
<ComboBox x:Name="cboAlarmType" HorizontalAlignment="Left" Margin="138,256,0,0" VerticalAlignment="Top" Width="320" TabIndex="5"
BorderBrush="Black" Background="White" ItemsSource="{Binding}" DisplayMemberPath="AlarmName" SelectedValuePath="AlarmName"
SelectedValue="{Binding Row.Column, ElementName=dgvMain}"/>
此方法适用于文本框和复选框,但似乎还没有为组合框解决。任何指导将不胜感激! :)
【问题讨论】:
-
如何填充 DataGrid?在 WPF 中,你不需要做这样丑陋的事情。只需转到您用于填充 DataGrid 的项目。此外,DataTemplate 中的多重绑定看起来像是猜测;您似乎没有做任何多重绑定将用于的事情。你用什么填充组合框?
-
我正在通过 DataTable 填充 DataGrid。 DataTable 中的数据来自我的 SQLite 数据库中的 Select 语句。
-
<DataTemplate><TextBlock Text="{Binding Column}" /></DataTemplate>应该和你在那里的一样。 -
我猜我将 Multibinding StringFormat 留在其中的原因是因为我一次合并了 2 列。你说得对,只需要一列。
-
哦,所以
DataRowView是数据表中的一行,抱歉(derp)。组合框是否填充?我想你想要的可能是<ComboBox DisplayMemberPath="Column" SelectedValuePath="Column" SelectedValue="{Binding Row.Column, ElementName=dgvMain}" ... />
标签: c# wpf data-binding combobox