Combobox INotifyPropertyChanged事件未提出!

时间:2010-05-26 06:37:03

标签: silverlight data-binding silverlight-3.0 inotifypropertychanged

我创建了一个组合框并将可观察集合设置为itemsource,并在可观察集合项上实现了INotifyPropertyChanged。即使在此之后,当我在组合框中选择不同的项目时,也不会调用OnPropertyChange方法。我想我没有正确地进行绑定。任何人都可以在这方面纠正我/建议我。

--------------------------------- MainPage.xaml中----------- ----------------------------------------

<UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  x:Class="MasterDetailsUpdate.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">

    <StackPanel Width="300">

            <ComboBox Name="cboName"></ComboBox>

            <TextBox Name="tbxName" Text="{Binding Path=name,Mode=TwoWay,ElementName=cboName}" ></TextBox>


    </StackPanel>

</UserControl>

--------------------------- MainPage.xaml.cs中--------------- --------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;

namespace MasterDetailsUpdate
{

    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            ObservableCollection<Person> persons = new ObservableCollection<Person>();
            persons.Add(new Person { city = "c1", name = "n1" });
            persons.Add(new Person { city = "c2", name = "n2" });
            persons.Add(new Person { city = "c3", name = "" });
            persons.Add(new Person { city = "c4", name = "" });
            persons.Add(new Person { city = "c5", name = "n1" });

            cboName.ItemsSource = persons;
            cboName.DisplayMemberPath = "name";
        }       
    }

    public class Person : INotifyPropertyChanged
    {
        private string _name;
        private string _city;
        public string name
        {
            set
            {
                _name = value;
                OnPropertyChanged("name");
            }
            get
            {
                return _name;
            }
        }


        public string city
        {
            set
            {
                _city = value;
                OnPropertyChanged("city");
            }
            get
            {
                return _city;
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}

谢谢

1 个答案:

答案 0 :(得分:1)

不,你没有绑定权,你的文本框绑定应如下所示: -

<TextBox Name="tbxName" Text="{Binding Path=SelectedItem.name, Mode=TwoWay, ElementName=cboName}" />

ComboBox没有name属性。它有一个SelectedItem属性,它将是当前选中的Person实例。然后,您需要该实例的name属性。因此,绑定中需要的路径是SelectedItem.name

现在您应该发现,当您编辑文本框将焦点移到其他位置时,PropertyChange事件将会触发,您应该会在组合框中看到更改。

默认情况下,文本框注释不会更新其值,直到用户按Tab键或以其他方式移动焦点。