logcat
logcat

Reputation: 485

How to get selected text from WPF Combobox?

I am trying to get text from WPF ComboBox, when it will select but unable to get any text. Below are the codes which I have tried. If someone please help me to get text from ComboBox when I select different Content. please note that, ComboBox will load data from sql server which is working perfectly !

my XAML code is:

<ComboBox x:Name="comboID" TabIndex="27" SelectedValuePath="Content" SelectionChanged="comboID_SelectionChanged"  HorizontalAlignment="Left" Margin="694,396,0,0" VerticalAlignment="Top" Width="165"/>

below are my C# codes example which I have tried:

string str1 = comboID.SelectedItem.ToString();

string str2 = comboID.SelectedValue.ToString();

string str3 = comboID.Text;

ComboBoxItem cmb = comboID.SelectedItem as ComboBoxItem;

var myVal = sender as ComboBox;
string value = comboID.SelectedItem as string;

Upvotes: 0

Views: 5801

Answers (4)

MattE
MattE

Reputation: 1114

Add the Tag property to the comboboxitems:

<Comboboxitem Tag="This Value"/>

Then

GetValue=ComboBoxName.SelectedItem.Tag.ToString()

GetValue will be "This Value" instead of the System.windows.combobox blah blah blah..

Upvotes: 0

Shalem
Shalem

Reputation: 1516

Simple code. Try this, should work

string s = comboID.text;

Upvotes: 0

user3740362
user3740362

Reputation:

Use this code instead of yours for adding items in combobox:

SqlConnection conn = new SqlConnection("your connection");

SqlCommand cmnd = new SqlCommand();

SqlDataReader sdr = null;

conn.Open();
cmnd.Connection = conn;

String query = "Select ID from Seller ORDER BY ID";                
cmnd.CommandText = query;

sdr = cmnd.ExecuteReader();

while (sdr.Read())
{
   comboID.Items.Add(sdr.GetString(0));
}

Now you can use:

string str = comboExporterID.SelectedItem.ToString();

Upvotes: 1

Sandesh
Sandesh

Reputation: 3004

I have created a simple code to give you a rough idea.

My Xaml

<Window x:Class="StackOverflow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="350">
    <StackPanel Orientation="Horizontal">
        <Label Content="Select Item" x:Name="LblSelectNumber" MinWidth="50px" Margin="5"></Label>
        <ComboBox x:Name="ComboId" DisplayMemberPath="Number"
                  SelectedValuePath="Content" SelectionChanged="ComboID_OnSelectionChanged" MinWidth="100"
                  HorizontalAlignment="Left" Margin="5" VerticalAlignment="Top" />
    </StackPanel>
</Window>

My code behind

public partial class MainWindow : Window
{
    private readonly List<MyItem> _items;
    public MainWindow()
    {
        InitializeComponent();

        _items = new List<MyItem>
            {
                new MyItem{Content = "Test1",Number = "One"},
                new MyItem{Content = "Test2",Number = "Two"},
                new MyItem{Content = "Test3",Number = "Three"}
            };

        ComboId.ItemsSource = _items;
    }

    private void ComboID_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        MessageBox.Show(ComboId.SelectedValue.ToString());
    }
}



public class MyItem
{
    public string Content { get; set; }
    public string Number { get; set; }
}

Please observe the usage of DisplayMemberPath property in ComboBox

A simple suggestion. Please use MVVM pattern while creating your application as it will help you to maintain the solution in the long run. What ever I have done above can be achieved quite easily using MVVM pattern

Upvotes: 0

Related Questions