简体   繁体   English

当我在WPF中选择一行数据网格时,如何在组合框中显示一个值?

[英]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_NameCamera_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中的T​​extBox和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.

相关问题 在WPF中的DataGrid中选择一行时如何打勾CheckBoxColumn - How to tick a CheckBoxColumn when I select a row in a DataGrid in WPF 在Datagrid行编辑,WPF上显示ComboBox - Show ComboBox on Datagrid row edit, WPF 当我使用WPF选中datagrid中的复选框时,如何获取行值? - How to get row value when I check checkbox in datagrid with WPF? 当ItemsSource为空时,如何确保WPF DataGrid至少显示一行? (可编辑的ComboBox单元格) - How can I make sure my WPF DataGrid shows at least one row when ItemsSource is empty? (Editable ComboBox cells) WPF DataGrid从文本框中选择带有值的行 - WPF DataGrid select row with value from textbox WPF Datagrid ComboBox 选项因另一行值而异 - WPF Datagrid ComboBox options different based on another row value WPF MVVM 如何将 ComboBox SelectedItem 绑定到特定的 DataGrid 选定行列值 - WPF MVVM How to bind ComboBox SelectedItem to specific DataGrid selected row column value 如何以编程方式在WPF DataGrid中选择行或单元格? - How to select a row or a cell in WPF DataGrid programmatically? 当我从其他表单中打开它时,组合框始终显示第一个值,而不是datagrid中的值 - the combobox show always the first value when i open it from another form not the value from datagrid 组合框值更改时,WPF MVVM更新数据网格 - WPF MVVM Update Datagrid when Combobox value changes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM