2
<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" }};

コンボボックスのテキストを編集しているときに、バインドされたデータ セットが変更されません。つまり、ターゲットからソースへのバインディングは行われていません。

4

1 に答える 1

1

Joshのアドバイスに追加する....最初に、diff変数名を使用してから「値」を使用することを検討する必要があります。次に
、値が変更されていない場合は「PropertyChanged」イベントを発生させないでください。

これをプロパティセッターに追加してください....

if ( value != this.value )
{

}

3番目、データのインスタンスにバインドしない、クラスタイプにバインドする

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

4番目に、コンボボックスのItemSourceをに設定する必要がありますObservableCollection< Number >

最後に、データ バインディングのデバッグに関する Bea の素晴らしいブログ エントリを確認してください。彼女には多くの素晴らしい例があります。

わかりました、これでコンパイラにアクセスできるようになりました....ここであなたがしなければならないことは次のとおりです。
まず、バインドしている「Number」プロパティはどこにありますか? コンボボックスのソースであるリストにバインドすることはできません。

ElementName をバインディングに追加するか、DataContext を Number プロパティを含むオブジェクトに設定する必要があります。次に、その Number プロパティは、それがどこであろうと、Notify または DependencyProperty のいずれかである必要があります。
たとえば、Window クラスは次のようになります.....

   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));

   }

window.xaml は次のようになります...

<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>
于 2010-02-04T07:00:15.090 に答える