Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a Two ComboBox in my application in which ComboBoxItems are "Yes" and "No".

I want to assign the text of my TextBox as "cleared" if the selected ComboBoxItem is "Yes" in both the combobox and "not cleared" if the selected ComboBoxItem is "No" in both or "yes" in one or "No" in other ComboBox How do I do that in WPF?
I have Done this way but its not working accuretly
XML
in my Xaml

<ComboBox Height="17" HorizontalAlignment="Left" IsEditable="False" IsReadOnly="False" Margin="297,103,0,0" Name="comboBox1" VerticalAlignment="Top" Width="101" SelectionChanged="comboBox1_SelectionChanged"/>
<ComboBox Height="17" HorizontalAlignment="Left" IsEditable="False" IsReadOnly="False" Margin="297,120,0,0" Name="comboBox2" VerticalAlignment="Top" Width="101" SelectionChanged="comboBox2_SelectionChanged"/>
<TextBox Height="19" HorizontalAlignment="Left" Margin="297,0,0,54" Name="textBox32" VerticalAlignment="Bottom" Width="101"  />

C#
Into my Xmal.Cs


{
            InitializeComponent();
            comboBox1.Items.Add("Yes");
            comboBox1.Items.Add("No");
            comboBox2.Items.Add("Yes");
            comboBox2.Items.Add("No");
}
private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            {
                textBox32.Text = (comboBox1.SelectedItem != null && comboBox1.SelectedItem.ToString() == "Yes") ?
                    "cleared" :

                    "not cleared";
            }
        }

        private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            {
                textBox32.Text = (comboBox2.SelectedItem != null && comboBox2.SelectedItem.ToString() == "Yes" ) ?
                    "cleared" :
                    "not cleared";
            }
        }
Posted
Updated 12-Feb-11 0:01am
v3

1 solution

WPF has such powerful binding capabilities that you should not have to handle control events as you do above. Here's a quickly put together example that shows you what you can do (please keep in mind that I have not done too much error checking, case-insensitive compare etc. - the code is just an example for you):

XML
<ComboBox ItemsSource="{Binding Strings}" SelectedItem="{Binding Combo1String}"
    Height="23" HorizontalAlignment="Left" Margin="36,35,0,0"
    Name="comboBox1" VerticalAlignment="Top" Width="120" />
<ComboBox ItemsSource="{Binding Strings}" SelectedItem="{Binding Combo2String}"
    Height="23" HorizontalAlignment="Left" Margin="202,35,0,0"
    Name="comboBox2" VerticalAlignment="Top" Width="120" />
<TextBox Text="{Binding Status}"
    Height="23" HorizontalAlignment="Left" Margin="42,90,0,0"
    Name="textBox1" VerticalAlignment="Top" Width="257" />


C#
public MainWindow()
{
  InitializeComponent();

  this.Strings = new[] { "yes", "no" };
  this.PropertyChanged += MainWindow_PropertyChanged;
}

void MainWindow_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
  if (e.PropertyName.StartsWith("Combo"))
  {
      if (this.Combo1String == "yes" & this.Combo1String == this.Combo2String)
      {
          this.Status = "Cleared";
      }
      else
      {
          this.Status = "Not cleared";
      }
  }
}

public string[] Strings { get; set; }

private string combo1String;

public string Combo1String
{
  get { return combo1String; }

  set
  {
      if (combo1String != value)
      {
          combo1String = value;
          this.FirePropertyChanged("Combo1String");
      }
  }
}

private string combo2String;

public string Combo2String
{
  get { return combo2String; }

  set
  {
      if (combo2String != value)
      {
          combo2String = value;
          this.FirePropertyChanged("Combo2String");
      }
  }
}

private string status;

public string Status
{
  get { return status; }

  set
  {
      if(status != value)
      {
          status = value;
          this.FirePropertyChanged("Status");
      }
  }
}


[Edit]
~~~~~~~

In response to your comment, you need to implement INotifyPropertyChanged. That's an absolute requirement for MVVM approaches.
 
Share this answer
 
v3
Comments
amitkarnik2211 14-Feb-11 0:29am    
Thanks Man This is working Perfectly Great
amitkarnik2211 15-Feb-11 2:32am    
Error is Been Showed to This Meathosd I m unable to Resolve it

this.PropertyChanged += MainWindow_PropertyChanged;What should I do
Nish Nishant 15-Feb-11 7:51am    
It's because you have not implemented INotifyPropertyChanged. Implement that.

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