WPF ComboBox绑定没有正确更新 [英] WPF ComboBox Binding not updating properly

查看:1899
本文介绍了WPF ComboBox绑定没有正确更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

>

更新问题与绑定在我的ComboBox。我已经创建了一个简化的例子说明这个问题。我将粘贴以下所有相关代码。

I have an updating issue with Binding in my ComboBox. I have created a simplified example illustrate this problem. I will paste all the related code below.

所以在上面的图片中,我有一个TextBlock(黑色)显示SelectedPerson.FullName属性。 SelectedPerson的FirstName和LastName属性显示在FullName下面的2个文本框中。 ComboBox DisplayMemberPath绑定到FullName,组合框包含一个Person对象列表。

So in the image above, I have a TextBlock (in black) that shows the 'SelectedPerson.FullName' property for the SelectedPerson. The FirstName and LastName properties of the SelectedPerson are shown in the 2 TextBoxes below the FullName. The ComboBox DisplayMemberPath is bound to 'FullName' and the combobox contains a list of Person objects.

FullName属性在TextBlock,ComboBox和ComboBox下拉列表。

The 'FullName' property should be the same in the TextBlock, ComboBox, and ComboBox dropdown list.

但是,它只是在TextBlock中正确更新。 comboBox下拉列表只是在第一次打开时更新,之后不更新。因此,如果我在文本框中编辑FirstName,然后点击下拉列表,然后再编辑一些,再次点击下拉列表,我们将为SelectedPerson显示3个不同的FullName值。

However, it is ONLY updating correctly in the TextBlock. The comboBox Dropdown just updates the first time it is opened and doesn't update after that. So if I edit the FirstName in the textbox and click the dropdown and then edit it some more and click the dropdown again, we end up with 3 different 'FullName' values being displayed for the SelectedPerson.

我在我的代码中使用INotifyPropertyChanged,当SelectedPerson改变时通知。这看起来工作正常更新TextBlock,但由于某种原因,ComboBox不更新与新的FullName。

I am using INotifyPropertyChanged in my code behind to notify when "SelectedPerson" is changed. This appears to work fine updating the TextBlock, but for some reason, ComboBox isn't updating with the new "FullName".

我试图发送一个FullName

I have attempted to send a "FullName" notify when I send 'SelectedPerson' notify, but it doesn't make it work either.

任何人都可以告诉我为什么ComboBox在更改FirstName时不会更新文本?

Can anybody tell me why the ComboBox isn't updating when I change the FirstName text?

感谢。

MainWindow.xaml:

MainWindow.xaml:

<Window x:Class="ComboBoxBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <StackPanel Margin="50" Width="150">
        <TextBlock Margin="0,0,0,10" Height="30" Padding="8" Background="Black" Foreground="White" Text="{Binding SelectedPerson.FullName}"/>
        <TextBox Text="{Binding SelectedPerson.FirstName, UpdateSourceTrigger=PropertyChanged}"/>
        <TextBox Text="{Binding SelectedPerson.LastName, UpdateSourceTrigger=PropertyChanged}"/>
        <ComboBox Margin="0,16,0,0"
                  ItemsSource="{Binding People}"
                  DisplayMemberPath="FullName"
                  SelectedItem="{Binding SelectedPerson, UpdateSourceTrigger=PropertyChanged}">
        </ComboBox>
    </StackPanel>
</Window>

MainWindow.xaml.cs:

MainWindow.xaml.cs:

using System.Collections.Generic;
using System.ComponentModel;

namespace ComboBoxBinding
{
    public partial class MainWindow : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

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

        private List<Person> _people = new List<Person>();

        public List<Person> People 
        {
            get { return _people; }
        }

        private Person _selectedPerson;

        public Person SelectedPerson
        {
            get { return _selectedPerson; }
            set 
            {
                _selectedPerson = value;
                OnPropertyChanged("SelectedPerson");
            }
        }

        public MainWindow()
        {
            GenerateLaneConfigurations();
            InitializeComponent();
        }

        private void GenerateLaneConfigurations()
        {
            People.Add(new Person("Elmer", "Fudd"));
            People.Add(new Person("Bugs", "Bunny"));
            People.Add(new Person("Donald", "Duck"));

            foreach (Person person in _people)
            {
                person.Changed += person_Changed;
            }

            SelectedPerson = People[0];
        }

        void person_Changed()
        {
            OnPropertyChanged("SelectedPerson");
        }
    }
}

Person.cs:

Person.cs:

namespace ComboBoxBinding
{
    public class Person
    {
        private string _firstName;
        public string FirstName
        {
            get { return _firstName; }
            set { _firstName = value; OnChanged(); }
        }

        private string _lastName;
        public string LastName
        {
            get { return _lastName; }
            set { _lastName = value; OnChanged(); }
        }

        public string FullName
        {
            get { return string.Format("{0} {1}", FirstName, LastName);}
        }

        public Person(string firstName, string lastName)
        {
            FirstName = firstName;
            LastName = lastName;
        }

        public delegate void ChangedEventHandler();
        public event ChangedEventHandler Changed;

        protected void OnChanged()
        {
            if (Changed != null)
            {
                Changed();
            }
        }
    }
}


推荐答案

您还需要在Person对象中实现通知系统。表示 SelectedPerson 更改不够好。只要在 MainWindow INotifyPropertyChanged >并且提高 PropertyChanged 事件,而不是您的自定义更改的事件,它应该工作。

You need to implement the notification system in your Person object as well. Saying that the SelectedPerson changed isn't good enough. Just implement INotifyPropertyChanged on Person like you already do on your MainWindow and raise the PropertyChanged event instead of your custom Changed event and it should work.

这篇关于WPF ComboBox绑定没有正确更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆