开发者

WPF Two Way DataBinding - an Editable ComboBox to a DataView

<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 : INotifyPropert开发者_JAVA百科yChanged
    {
        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.


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜