未引发组合框 INotifyPropertyChanged 事件!

发布于 2024-09-03 11:22:49 字数 3427 浏览 1 评论 0原文

我创建了一个组合框并将可观察集合设置为项目源,并在可观察集合项目上实现了 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
    }
}

谢谢

I created a combobox and set observable collection as the itemsource and implemented INotifyPropertyChanged on the observable collection item. Even after that, when I select different item in the combobox, the OnPropertyChange method is not invoked. I think I am not making the binding properly. Could any one please correct me/ suggest me in this regard.

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

Thank You

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

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

发布评论

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

评论(1

彡翼 2024-09-10 11:22:49

不,您的绑定不正确,您的文本框绑定应该如下所示:-

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

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

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

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

No you aren't binding right, your textbox binding should look like this:-

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

A ComboBox doesn't have a name property. It does have a SelectedItem property which will be the Person instance currently selected. You then want the name property of that instance. Hence the path you need in the binding is SelectedItem.name.

Now you should find that when you edit the textbox and move the focus elsewhere then PropertyChange event will fire and you should see the change in the combo box.

Note by default textbox does not update its value until the user presses tab or in some other way moves the focus on.

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