WPF 双向数据绑定 - 数据视图的可编辑组合框

发布于 2024-08-20 09:00:51 字数 1068 浏览 7 评论 0原文

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

当我编辑组合框文本时,我的绑定数据集没有修改。 即,目标到源的绑定没有发生。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦忆晨望 2024-08-27 09:00:51

添加 Josh 建议的内容......
首先,您应该考虑使用 diff 变量名称,然后使用“值”,
其次,如果值没有更改,则不应触发“PropertyChanged”事件。

将其添加到属性设置器...

if ( value != this.value )
{

}

第三,您不绑定到数据实例,而是绑定到类类型

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

第四,您应该将组合框中的 ItemSource 设置为 ObservableCollectionObservableCollectionNumber >

最后,您应该查看 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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文