WPF数据绑定是否会更改UI线程?

bit*_*onk 16 data-binding wpf multithreading mvvm

我刚刚注意到,当我ViewModel从后台工作线程更改我的(MVVM)中的绑定属性时,我没有得到任何异常,并且视图已正确更新.这是否意味着我可以安全地依赖于wpf数据绑定编组ViewModelUI线程中的所有更改?我想我已经读过一个应该确保(在ViewModel)INotifyPropertyChanged.PropertyChanged线程上触发的内容.这有什么改变在3.5或什么?

Ken*_*art 14

对于标量是肯定的,对集合没有.对于集合,您需要一个专门的集合为您编组,或者通过自己手动编组到UI线程Dispatcher.

你可能已经读过INotifyCollectionChanged.CollectionChanged必须触发UI线程,因为它根本不是真的INotifyPropertyChanged.PropertyChanged.下面是一个非常简单的示例,证明了WPF为您调整属性更改.

Window1.xaml.cs:

using System.ComponentModel;
using System.Threading;
using System.Windows;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        private CustomerViewModel _customerViewModel;

        public Window1()
        {
            InitializeComponent();
            _customerViewModel = new CustomerViewModel();
            DataContext = _customerViewModel;

            var thread = new Thread((ThreadStart)delegate
            {
                while (true)
                {
                    Thread.Sleep(2000);
                    //look ma - no marshalling!
                    _customerViewModel.Name += "Appended";
                    _customerViewModel.Address.Line1 += "Appended";
                }
            });

            thread.Start();
        }
    }

    public abstract class ViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    public class CustomerViewModel : ViewModel
    {
        private string _name;
        private AddressViewModel _address = new AddressViewModel();

        public string Name
        {
            get { return _name; }
            set
            {
                if (_name != value)
                {
                    _name = value;
                    OnPropertyChanged("Name");
                }
            }
        }

        public AddressViewModel Address
        {
            get { return _address; }
        }
    }

    public class AddressViewModel : ViewModel
    {
        private string _line1;

        public string Line1
        {
            get { return _line1; }
            set
            {
                if (_line1 != value)
                {
                    _line1 = value;
                    OnPropertyChanged("Line1");
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Window1.xaml:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel>
        <TextBox Text="{Binding Name}"/>
        <TextBox Text="{Binding Address.Line1}"/>
    </StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)