【问题标题】:Binding Text property in ComboBox not working组合框中的绑定文本属性不起作用
【发布时间】:2012-03-15 12:19:50
【问题描述】:

我有一个包含多个ComboBoxes 的 WPF 应用程序。一些组合框的ItemsSource 绑定到对象列表。我想将每个组合框的文本属性绑定到MyObject 的某个属性。每次用户在MyListView 中选择某行时,我都会更新MyObject 的属性,并且我希望组合框的文本属性也能更新。

这是其中一个组合框的 XAML:

<StackPanel Orientation="Vertical" x:Name="StackPanel_MyStackPanel">
    <ComboBox x:Name="comboBox_MyComboBox"
              IsEditable="True"
              ItemsSource="{Binding}"
              Text="{Binding Path=MyProperty}" /> 
</StackPanel>

在后面的代码中:

MyObject myObject = new MyObject();

// On the selection changed event handler of the MyListView,
// I update the MyProperty of the myObject.

this.StackPanel_MyStackPanel.DataContext = myObject;

MyObject的定义:

public class MyObject
{
    private string _MyProperty;

    public string MyProperty
    {
        get { return _MyProperty; }
        set { _MyProperty = value; }
    }
}

这不起作用....我不知道为什么。

【问题讨论】:

  • 在分配this.StackPanel_MyStackPanel.DataContext = myObject 之前或之后,您具体何时更新myObject.MyProperty
  • 我想将组合框的 Text 属性绑定到 MyObject.MyProperty ,组合框的 ItemSource 绑定到我后面代码中的某个集合 - 我这里没有提到是什么集合。跨度>
  • @Clemens 在我分配 this.StackPanel_MyStackPanel.DataContext = myObject 之后
  • 那么MyObject类需要实现INotifyPropertyChanged并在MyProperty变化时引发PropertyChanged事件。
  • 好吧.....你说得对......我现在就去做,并告诉你它是否解决了我的问题......

标签: wpf c#-4.0 data-binding combobox wpf-controls


【解决方案1】:

你的数据类需要实现INotifyPropertyChanged

public class MyObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private string _MyProperty; 
    public string MyProperty
    { 
        get { return _MyProperty;} 
        set
        {
            _MyProperty = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("MyProperty"));
            }
        } 
    } 
} 

【讨论】:

    【解决方案2】:

    对我来说它正在工作..

    顺便说一句,ItemsSource是组合框中的项目,这里不需要设置

    我添加了一个按钮来测试它...这是我的代码隐藏:

    MyObject myObject = new MyObject();
    
    /// <summary>
    /// Initializes a new instance of the <see cref="MainView"/> class.
    /// </summary>
    public MainView()
    {
        InitializeComponent();
    
    
        //On the selection changed event handler of the MyListView , I update the 
        //MyProperty of the myObject.
    
        this.StackPanel_MyStackPanel.DataContext = myObject;
    
    }
    
    private void test_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        MessageBox.Show(myObject.MyProperty);
    }
    

    我的 XAML:

    <StackPanel x:Name="StackPanel_MyStackPanel"
                Width="Auto"
                Height="Auto"
                Orientation="Vertical">
        <ComboBox x:Name="comboBox_MyComboBox"
                  IsEditable="True"
                  Text="{Binding Path=MyProperty}" />
        <Button Name="test" Click="test_Click" Content="Show it" />
    </StackPanel>
    

    我采用了您的 MyObject 实现,但将您的局部变量重命名为 _MyProperty - 它是 MyPropety

    【讨论】:

      猜你喜欢
      • 2010-11-11
      • 2013-02-19
      • 1970-01-01
      • 1970-01-01
      • 2011-01-28
      • 2015-04-10
      • 2011-07-25
      • 2015-08-12
      • 2012-03-15
      相关资源
      最近更新 更多