Click here to Skip to main content
15,884,995 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
I have a ComboBox in my application in which ComboBoxItems are "Yes" and "No".

I want to assign the text of my TextBox as "cleared" if if the selected ComboBoxItem is "Yes" and "not cleared" if the selected ComboBoxItem is "No". How do I do that in WPF?
Posted
Updated 10-Feb-11 11:46am
v2

Assuming first item is "Yes" and second "No", you do something like this:

C#
MyComboBox.SelectionChanged += (sender, eventArgs) => {
    if (cb.SelectedIndex == 0)
        this.MyTextBox.Text = "Cleared";
    else
        this.MyTextBox.Text = "No, not cleared!";
}; //MyComboBox.SelectionChanged


That's it.

—SA
 
Share this answer
 
v2
You can do this with a DataTrigger:
XML
<Grid>
    <ComboBox VerticalAlignment="Top" HorizontalAlignment="Left" SelectedIndex="0" Name="cboBool">
        <ComboBoxItem>Yes</ComboBoxItem>
        <ComboBoxItem>No</ComboBoxItem>
    </ComboBox>
    <TextBox VerticalAlignment="Bottom">
        <TextBox.Style>
            <Style>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=cboBool, Path=SelectedValue.Content}" Value="Yes">
                        <Setter Property="TextBox.Text" Value="Cleared" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding ElementName=cboBool, Path=SelectedValue.Content}" Value="No">
                        <Setter Property="TextBox.Text" Value="Not Cleared" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>
</Grid>
 
Share this answer
 
Comments
Keith Barrow 10-Feb-11 15:16pm    
Top-notch answer, 5'd. Hope the OP uses this as it keeps the view stuff in the view.
Sergey Alexandrovich Kryukov 10-Feb-11 17:25pm    
Good one, voted 5,
--SA
 
Share this answer
 
Comments
AspDotNetDev 10-Feb-11 13:21pm    
Huh?
Assuming that I have a combobox named MyComboBox
<TextBox Text="{Binding Path=SelectedValue, ElementName=MyComboBox}" /> 


I can't think of any way to make it simpler :)

Update
Populate the combobox using a collection with an element type with two text properties one for the cobobox and one for the texbox. override the ToString() on the element type to return the text for the TextBox, and use an ItemTemplate to display the correct text in the combobox.

Regards
Espen Harlinn
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 10-Feb-11 17:00pm    
Good solution (a 5), even though it won't show arbitrary text in this text box as OP wanted.
I did exactly what OP wanted, but I would not design that way because I would not like those ad-hoc text data.
--SA
Espen Harlinn 10-Feb-11 17:08pm    
Thank you SAKryukov!
I did a quick example - but this should be easy - improve your basics!

Start with an empty WPF-Project
Replace the markup in Window1.xaml with this:

XML
<Window x:Class="WpfApplication.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <ComboBox Height="23" Margin="46,30,112,0" Name="comboBox1" VerticalAlignment="Top" SelectionChanged="comboBox1_SelectionChanged" />
        <TextBox Margin="46,125,112,114" Name="textBox1" />
    </Grid>
</Window>


... and replace the code in Window1.xaml.cs with:

C#
using System;
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            comboBox1.Items.Add("Yes");
            comboBox1.Items.Add("No");
        }

        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            textBox1.Text = (comboBox1.SelectedItem != null && comboBox1.SelectedItem.ToString() == "Yes") ?
                "cleared" :
                "not cleared";
        }
    }
}
 
Share this answer
 
This simple code works
C#
if (comboBox1.SelectedItem == "Yes")
            {
                textBox1.Text = "cleared";
            }
            else { textBox1.Text = "not cleared"; }
 
Share this answer
 
Comments
amitkarnik2211 10-Feb-11 7:04am    
Hi Thanks For the Reply I tried the Same But is Giving this Error

(Possible unintended reference comparison; to get a value comparison, cast the left hand side to type 'string')
m@dhu 10-Feb-11 7:49am    
if (comboBox1.SelectedItem.Equals("yes"))
{
textBox1.Text = "cleared";
}
else{ textBox1.Text = "not cleared"; }
amitkarnik2211 10-Feb-11 8:18am    
Hi Tried This way But after Selection it is just showinig not cleared selecting either yes or no when we run the application
private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (comboBox1.SelectedItem.Equals("yes"))
{
textBox32.Text = "cleared";
}
else { textBox32.Text = "not cleared"; }
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900