views:

354

answers:

1
<ComboBox Height="23" Margin="52,64,33,0" Name="comboBox1"  
              IsSynchronizedWithCurrentItem="True"
              IsEditable="True"
              DisplayMemberPath="Value" 
              SelectedItem="{Binding Path=Number, Mode=TwoWay}"
              />

public class Number : INotifyPropertyChanged
    {
        private string value;
        public string Value
        {
            get
            {
                return value;
            }
        set
        {
            this.value = value;
            this.PropertyChanged(this, new PropertyChangedEventArgs("Value"));
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    #endregion
}

 comboBox1.ItemsSource = new Number[] { new Number() { Value = "One" },
                                                   new Number() { Value = "Two" },
                                                   new Number() { Value = "Three" }};

My binded data set is not modifying when i am editing combobox text. ie., target to source binding is not happening.

Thanks in advance

A: 

adding to what Josh advises.... first, you should think about using a diff variable name then "value",
second, you shouldnt fire the "PropertyChanged" event if the value is not changing.

add this to the property setter....

if ( value != this.value )
{

}

third, your not binding to an instance of your data, your binding to your class type

SelectedItem="{Binding Path=Number, Mode=TwoWay}"

fourth, you should set the ItemSource in your combobox to an ObservableCollection< Number >

lastly, you should check out Bea's great blog entry about debugging databinding. She has many great examples.

ok, so now that i have access to my compiler.... here is what you need to do.
Firstly, WHERE is the "Number" property located that you are binding to? you can not bind back to the list that is the source of your combobox.

you need to add an ElementName to the binding, or set the DataContext to the object that contains the Number property. Second, that Number property, wherever it might be, needs to be either a Notify or a DependencyProperty.
for example, your Window class would look like this.....

   public partial class Window1 : Window
   {
      public Number Number
      {
         get { return (Number)GetValue(ValueProperty); }
         set { SetValue(ValueProperty, value); }
      }

      public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(Number),typeof(Window1), new UIPropertyMetadata(null));

   }

and your window.xaml would look like this...

<Window x:Class="testapp.Window1"
          x:Name="stuff"
    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="52,64,33,0" Name="comboBox1"  
              IsEditable="True"
              DisplayMemberPath="Value" 
              SelectedItem="{Binding ElementName=stuff, Path=Number, Mode=TwoWay}"
        />
    </Grid>
</Window>
Muad'Dib
I tried binding to SelectedItem property. But when i set the Text property of combobox then the SelectedItem property is getting null.
Subindev
IsTextSearchEnabled="True" caused the SelctedItem Property to get null whenever i tried setting the Text property. TextSearchEnabled="False" shown no problems with the 2 way binding to text to selecteditem propertybinding.. :-)
Subindev