繁体   English   中英

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

[英]How to show a value in combobox when i select a row of datagrid in wpf?

我在wpf的窗口中有一个daetagrid,一个组合框和一个文本框。

在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>

我得到名为mytbl的表的所有字段,并使用以下代码在datagrid中显示它们:

    private void window_Loaded(object sender, RoutedEventArgs e)
    {
        var q= from j in CamDB.mytbl select q;
        mydgv.ItemsSource = q.ToList();
    }

mytbl是一个表,它具有两个字段: Camera_NameCamera_Type 现在,当我选择一行数据网格时,我想在文本框中显示Camera_Name值, Camera_Type在组合框中显示Camera_Name值。 我阅读了此链接,并尝试了以下代码,但它适用于文本框,而不适用于组合框。

    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();

您是否尝试过绑定?

尝试将它们添加到xaml中的T​​extBox和ComboBox中:

<TextBox Text="{Binding ElementName=mydgv,Path=SelectedItem.Camera_Name" />
<ComboBox Text="{Binding ElementName=mydgv,Path=SelectedItem.Camera_Type" />

其他事情被省略。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM