通过INotifyPropertyChanged写入TextBox不起作用

时间:2015-07-24 03:34:27

标签: c# wpf

我遇到了调用INotifyPropertyChanged事件的属性的问题。我创建了一个简单的概念WPF程序,它试图通过一个按钮调用文本框,该按钮调用一个更新属性的函数(ViewModel1StringProperty)。该属性绑定到文本框,该属性调用INotifyPropertyChanged事件。我已将此文本属性添加到文本框中:

Text="{Binding ViewModel1StringProperty, UpdateSourceTrigger=PropertyChanged}"

但是,单击按钮时仍未更新文本框。需要注意的一点是按钮调用ICommand,然后最终通过函数调用(TestFunction1)更新属性。我相信这个命令正确绑定到按钮,但我不确定。

非常感谢对这个问题的任何见解。

ViewModel代码:

    namespace WpfApplicationOnPropertyChanged.ViewModel
    {
        class ViewModel1 : INotifyPropertyChanged
        {
            #region Fields
            string _viewModel1StringProperty;
            public event PropertyChangedEventHandler PropertyChanged;
            #endregion

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

        #region Properties
        public string ViewModel1StringProperty
        {
            get
            {
                return _viewModel1StringProperty;
            }

            set
            {
                _viewModel1StringProperty = value;
                NotifyPropertyChanged("ViewModel1StringProperty");
            }
        }
        #endregion

        #region Commands
        public ICommand ViewModelICommandField1
        {
            get
            {
                return new ViewModelICommand(TestFunction1);
            }
        }
        #endregion

        #region Functions

        private void TestFunction1()
        {
            ViewModel1StringProperty = "Test1\n";
        }
        #endregion
    }
}

查看代码:

    namespace WpfApplicationOnPropertyChanged.View
    {
        /// <summary>
        /// Interaction logic for View1.xaml
    /// </summary>
    public partial class View1 : UserControl
    {
        #region Fields
        ViewModel1 _viewModel1 = new ViewModel1();
        #endregion

        public View1()
        {
            InitializeComponent();
            this.DataContext = _viewModel1;
        }
    }
}

XAML代码:

    <UserControl x:Class="WpfApplicationOnPropertyChanged.View.View1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml   /presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApplicationOnPropertyChanged.View"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <TextBox Name="textBox1" HorizontalAlignment="Left" Height="89" Margin="58,62,0,0" Text="{Binding ViewModel1StringProperty, UpdateSourceTrigger=PropertyChanged}" TextWrapping="Wrap" VerticalAlignment="Top" Width="181"/>
        <Button Content="Button1"  Command="{Binding ViewModel1ICommandField1}" HorizontalAlignment="Left" Margin="106,205,0,0" VerticalAlignment="Top" Width="75"/>
    </Grid>
</UserControl>

ICommand代码:

    namespace WpfApplicationOnPropertyChanged.ViewModel
    {
        class ViewModelICommand : ICommand
        {
           private Action _action;

        public ViewModelICommand(Action action)
        {
            _action = action;
        }    

        public void Execute(object parameter)
        {
            _action();
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged
        {
            add { }
            remove { }
        }
    }
}

1 个答案:

答案 0 :(得分:3)

在XAML中,您已将按钮命令绑定到ViewModel1ICommandField1,但在视图模型中,其名称为ViewModelICommandField1

请重命名以使其匹配,并且您将获得正确的行为。