【问题标题】:Writing to TextBox via INotifyPropertyChanged not working通过 INotifyPropertyChanged 写入 TextBox 不起作用
【发布时间】:2015-07-24 03:34:26
【问题描述】:

我遇到了调用 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>

I命令代码:

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

【问题讨论】:

  • 如果对你有帮助,你可以接受我的回答:)

标签: c# wpf


【解决方案1】:

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

请重命名其中一个,使它们匹配,您将获得正确的行为。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    • 2019-06-29
    • 2011-11-26
    相关资源
    最近更新 更多