C# WPF Combobox 有两列,如何获取所选项目?

问题描述 投票:0回答:1

我有一个组合框:

                            <ComboBox x:Name="cmbOrganization_Config_AddSalGrp"
                                      FontSize="20"
                                      Margin="5 0 5 5"
                                      SelectedItem="{Binding OrgID}">
                                <ComboBox.ItemTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal">
                                            <TextBlock Text="{Binding Organization_Name}" Margin="0 0 20 0" Visibility="Visible"/>
                                            <TextBlock Text="{Binding OrgID}" Visibility="Visible"/>
                                        </StackPanel>
                                    </DataTemplate>
                                </ComboBox.ItemTemplate>
                            </ComboBox>

我有这个隐藏代码:

        try
        {
            // Let the SqlCommand object know which stored procedure to execute
            SqlCommand cmd = new SqlCommand("tuc_salary_grouping_add", con);

            //Specify that it is a stored procedure and not a normal proc
            cmd.CommandType = System.Data.CommandType.StoredProcedure;

            //
            cmd.Parameters.AddWithValue("@SalaryGroupingName", this.SalaryGroupingConfig_add.Text);
            cmd.Parameters.AddWithValue("@Organization_ID", this.cmbOrganization_Config_AddSalGrp.SelectedItem);
            cmd.Parameters.AddWithValue("@Organization_ID", this.cmbOrganization_Config_AddSalGrp.Text);
            cmd.Parameters.AddWithValue("@Organization_ID", (this.cmbOrganization_Config_AddSalGrp.SelectedItem as ComboBoxItem).Content.ToString());
            cmd.Parameters.AddWithValue("@Notes", string.Empty);

            // Open the connection and execute the stored procedure
            con.Open();
            int rowsAffected = cmd.ExecuteNonQuery();
            Debug.WriteLine($"SQL> New record insert was successfully - [{rowsAffected}]");
            return true;

        }

用户选择该项目后,我需要从组合框中获取“OrgID”。

我尝试了3种不同的方法,但都不起作用。我的代码不是 MVVM 形式。

OrgID 是一个整数。我感谢你的帮助。

c# wpf combobox desktop-application selecteditem
1个回答
0
投票

您可以使用 SelectionChanged 事件来检索所选项目。就像下面的例子:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        var Items = new List<ItemsOrga>
        {
            new ItemsOrga("Id1", "Orga1"),
            new ItemsOrga("Id2", "Orga2"),
            new ItemsOrga("Id3", "Orga3")
        };
        cmbOrganization_Config_AddSalGrp.ItemsSource = Items;
    }

    private void cmbOrganization_Config_AddSalGrp_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (e.AddedItems.Count > 0)
        {
            var SelectedItem = e.AddedItems[0] as ItemsOrga;
            Debug.WriteLine(SelectedItem.OrgID);
        }
        
    }
}

public class ItemsOrga
{  
    public string Organization_Name { get; set; }
    public string OrgID { get; set; }
    public ItemsOrga(string OrgID, string Organization_Name)
    {
        this.OrgID = OrgID;
        this.Organization_Name = Organization_Name;
    }
}

© www.soinside.com 2019 - 2024. All rights reserved.