Rookie
Rookie

Reputation: 9

Combobox selection events

Good day. I making some programs in Windows forms, and now start to investigate new beautiful world of WPF. The first problem i face that WPF combobox working other way. My C# code:

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

    private void comboMenu_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (comboMenu.Text == "Meat soup")
        {
            textPrice.Text = "2.5";
            textDescription.Text = "Tasty and hearty soup with pieces of beaf and pork";
        }
        if (comboMenu.Text == "Vegetable soup")
        {
            textPrice.Text = "1.5";
            textDescription.Text = "Light and healthy soup with pieces of potato";
        }
        if (comboMenu.Text == "Chicken roll")
        {
            textPrice.Text = "3.5";
            textDescription.Text = "Fresh vegetables with pieces of chiken";
        }
    }
}

And XAML code like this:

ComboBox x:Name="comboMenu" HorizontalAlignment="Left" Margin="26,30,0,0" VerticalAlignment="Top" Width="234" Grid.Row="2" Grid.Column="1" SelectionChanged="comboMenu_SelectionChanged">

        <ComboBoxItem Content="Meat soup"></ComboBoxItem>
        <ComboBoxItem Content="Vegetable soup"></ComboBoxItem>
        <ComboBoxItem Content="Chicken roll"></ComboBoxItem>
    </ComboBox>

Here the problem. Combobox ignores first selected value at all and dont change any text in textboxes. Than when i select second time something, textboxes get confused and the menu choice dont apply to description. I feel that its something in XAML code need to change. Please help.

Upvotes: 0

Views: 72

Answers (3)

Rookie
Rookie

Reputation: 9

I solve this problem like ASh said, but XAML in my case looks little different.

<TextBlock Name="textPrice" DataContext="{Binding SelectedItem, ElementName=comboMenu}" Text="{Binding Path=Name}"/>

<TextBlock Name="textDescription" DataContext="{Binding SelectedItem, ElementName=comboMenu}" Text="{Binding Path=Description}"/>

In case its is useful for somebody.

Upvotes: 0

Clemens
Clemens

Reputation: 128147

Do not check comboMenu.Text, but comboMenu.SelectedItem, which is a ComboBoxItem in your case:

var selectedItem = (ComboBoxItem)comboMenu.SelectedItem;
var selectedText = (string)selectedItem.Content;

If you set

<ComboBox SelectedValuePath="Content" ...>

you can also use the SelectedValue property:

var selectedText = (string)comboMenu.SelectedValue;

With the XAML namespace dclaration

xmlns:sys="clr-namespace:System;assembly=mscorlib"

you can also use string items instead of ComboBoxItem

<ComboBox ...>
    <sys:String>Meat soup</sys:String>
    <sys:String>Vegetable soup</sys:String>
    <sys:String>Chicken roll</sys:String>
</ComboBox>

where you now get a SelectedItem that is string:

var selectedText = (string)comboMenu.SelectedItem;

You may also use a switch statement instead of multiple if statements:

switch (selectedText)
{
    case "Meat soup":
        textPrice.Text = "2.5";
        textDescription.Text = "Tasty and hearty soup with pieces of beaf and pork";
        break;
    case "Vegetable soup":
        textPrice.Text = "1.5";
        textDescription.Text = "Light and healthy soup with pieces of potato";
        break;
    case "Chicken roll":
        textPrice.Text = "3.5";
        textDescription.Text = "Fresh vegetables with pieces of chiken";
        break;
}

Upvotes: 0

ASh
ASh

Reputation: 35733

WPF allows to focus on data without much manipulations with UI elements.

Declare a class which hold information about menu items:

public class Food
{
    public string Name { get; set; }
    public string Description { gt; set; }
    public double Price { get; set; }
}

fill menu combobox with menu items:

<ComboBox x:Name="comboMenu" DisplayMemberPath="Name" HorizontalAlignment="Left" Margin="26,30,0,0" VerticalAlignment="Top" Width="234" Grid.Row="2" Grid.Column="1">
    <local:Food Name="Meat soup" Price="2.5" Description="Tasty and hearty soup with pieces of beaf and pork"/>
    <local:Food Name="Vegetable soup" Price="1.5" Description="Light and healthy soup with pieces of potato"/>
</ComboBox>

use Bindings to SelectedItem instead of selection event to update information about current selection:

<TextBlock Name="textPrice" Text="{Binding SelectedItem.Price, ElementName=comboMenu}"/>

<TextBlock Name="textDescription" Text="{Binding SelectedItem.Description, ElementName=comboMenu}"/>

Upvotes: 1

Related Questions