【问题标题】:WPF Two Way DataBinding - an Editable ComboBox to a DataViewWPF 双向数据绑定 - 数据视图的可编辑组合框
【发布时间】:2010-02-04 06:38:37
【问题描述】:
<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" }};

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

【问题讨论】:

    标签: wpf data-binding combobox two-way-binding


    【解决方案1】:

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

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

    if ( value != this.value )
    {
    
    }
    

    第三,你没有绑定到你的数据实例,你绑定到你的类类型

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

    第四,您应该将组合框中的 ItemSource 设置为 ObservableCollection&lt; Number &gt;

    最后,你应该看看Bea's great blog entry about debugging databinding.她有很多很好的例子。

    好的,现在我可以访问我的编译器了......这是你需要做的。
    首先,您要绑定的“数字”属性在哪里?您不能绑定回作为组合框来源的列表。

    您需要将 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>
    

    【讨论】:

    • 我尝试绑定到 SelectedItem 属性。但是,当我设置组合框的 Text 属性时,SelectedItem 属性将变为 null。
    • 每当我尝试设置 Text 属性时,IsTextSearchEnabled="True" 都会导致 SelectedItem 属性为空。 TextSearchEnabled="False" 显示将文本绑定到选定项属性绑定的 2 方式没有问题.. :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2011-08-13
    • 2015-12-12
    • 2014-02-03
    • 2011-08-25
    • 1970-01-01
    相关资源
    最近更新 更多