[英]How to show a value in combobox when i select a row of datagrid in wpf?
I have a daetagrid and a combobox and a textbox in a window in wpf. 我在wpf的窗口中有一个daetagrid,一个组合框和一个文本框。
in xmal: 在xmal中:
<TextBox x:Name="txtCameraName"/>
<ComboBox x:Name="cmbCameraType" SelectedValuePath="Camera_Type" IsEditable="True" IsReadOnly="True" Text="Please select...">
<ComboBoxItem>IP</ComboBoxItem>
<ComboBoxItem>webcam</ComboBoxItem>
<ComboBoxItem>analogue</ComboBoxItem>
</ComboBox>
<DataGrid x:Name="mydgv" SelectionChanged="dgvAddPersonTab_SelectionChanged">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Camera_Name}" Width="80" />
<DataGridTextColumn Binding="{Binding Camera_Type}" Width="80" />
</DataGrid.Columns>
I get all fields of a table named mytbl and show them in datagrid with this code: 我得到名为mytbl的表的所有字段,并使用以下代码在datagrid中显示它们:
private void window_Loaded(object sender, RoutedEventArgs e)
{
var q= from j in CamDB.mytbl select q;
mydgv.ItemsSource = q.ToList();
}
and mytbl
is a table that it has two field : Camera_Name
, Camera_Type
. 和
mytbl
是一个表,它具有两个字段: Camera_Name
和Camera_Type
。 Now I want to show value of Camera_Name
in textbox and value of Camera_Type
in combo when I select a row of datagrid. 现在,当我选择一行数据网格时,我想在文本框中显示
Camera_Name
值, Camera_Type
在组合框中显示Camera_Name
值。 I read this link and I tried with following code but it worked about textbox while it didn't work about combo. 我阅读了此链接,并尝试了以下代码,但它适用于文本框,而不适用于组合框。
private void mydgv_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var AllFields = mydgv.SelectedItem;
txtCameraName.Text = AllFields.GetType().GetProperty("Camera_Name").GetValue(AllFields, null).ToString();
cmbCameraType.SelectedValue = AllFields.GetType().GetProperty("Camera_Type").GetValue(AllFields, null);
}
我用下面的代码,它的工作原理:
cmbCameraType.Text = AllFields.GetType().GetProperty("Camera_Type").GetValue(AllFields, null).ToString();
Have you tried binding? 您是否尝试过绑定?
Try adding these to the TextBox and ComboBox in xaml: 尝试将它们添加到xaml中的TextBox和ComboBox中:
<TextBox Text="{Binding ElementName=mydgv,Path=SelectedItem.Camera_Name" />
<ComboBox Text="{Binding ElementName=mydgv,Path=SelectedItem.Camera_Type" />
Other things were omitted. 其他事情被省略。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.