【问题标题】:MVVM Button Content Properties not workingMVVM 按钮内容属性不起作用
【发布时间】:2015-11-10 11:05:19
【问题描述】:

我有这个代码

private string _contentButtonDisplaySearch;

public string ChangeContentButtonDisplaySearch
{
    get { return _contentButtonDisplaySearch; }
    set
    {
        if (_contentButtonDisplaySearch == value)
            return;
        _contentButtonDisplaySearch = value;
        OnPropertyChanged();
    }
}
<Button x:Name="ButtonDisplaySearch" Content="{Binding ChangeContentButtonDisplaySearch}"
        HorizontalAlignment="Left" Margin="188,0,0,10" VerticalAlignment="Bottom"
        Width="164" Height="39"/>

当我更改 _contentButtonDisplaySearch 的值时,我的按钮上的内容不会改变。我做错了什么?

这就是我更改 _contentButtonDisplaySearch 值的方式

private void OKRun()
{
    _contentButtonDisplaySearch = "ChangeContent";
}

INotifyPropertyChanged

#region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName)); 
        }

        #endregion

我设置 DataContext 这不起作用

<Button x:Name="ButtonDisplaySearch"  Content="{Binding ChangeContentButtonDisplaySearch}" DataContext="{Binding}" HorizontalAlignment="Left" Margin="188,0,0,10" VerticalAlignment="Bottom" Width="164" Height="39"/>

DataContext 设置在我的视图中

 this._view.SetDataContext(this);
            this._view.ShowIView();

当加载 MainWindow 时,当我设置 _contentButtonDisplaySearch 时,按钮内容发生变化,但是当我尝试更改时没有任何反应

【问题讨论】:

  • 您需要更改ChangeContentButtonDisplaySearch,而不是_contentButtonDisplaySearch,否则OnPropertyChanged() 将不会被调用。
  • 如果您设置了默认文本,您能否将您的文本视为按钮的内容?

标签: c# wpf mvvm data-binding


【解决方案1】:

直接修改属性而不是其值。如果修改变量set不会被触发,OnPropertyChange();事件也不会被触发。

尝试直接通过属性赋值:

private void OKRun()
  {
     ChangeContentButtonDisplaySearch = "ChangeContent";
  }

xaml 中的 &lt;Button/&gt; 应该有 UpdateSourceTrigger 属性设置如下:

<Button Content="{Binding ChangeContentButtonDisplaySearch, UpdateSourceTrigger=PropertyChanged}"/>

另外,尝试添加DataContext 有很多方法可以做到这一点。您可以尝试通过覆盖App.xaml.cs 文件中的OnStartup(); 事件来添加它。

protected override void OnStartup(StartupEventArgs e)
  {
        base.OnStartup(e);
        MainWindow _window = new MainWindow();
        ClassWhereIsYourPropertyStored _vM = new ClassWhereIsYourPropertyStored();
        _window.DataContext = _vM;
        _window.Show();
  }

【讨论】:

  • @VladimirKombarov 你设置DataContext了吗?
  • 是否需要DataContext?
  • @VladimirKombarov 是的,如果你想绑定properties 值,你需要设置DataContext
  • @VladimirKombarov 如果Content="{Binding ChangeContentButtonDisplaySearch,下方有错误行,则表示您没有正确设置DataContext
【解决方案2】:

我真的很感兴趣为什么Button 控件的Content 属性实际上没有更新。我找到了原因。因为string 类型是不可变的,如果条件if(_contentButtonDisplaySearch!= value) 不合适,因为我们只有新的string 对象,而这个新创建的string 对象将始终等于属性的value

您的视图模型:

public class YourViewModel:ViewModelBase
{
    int _contentButtonDisplaySearch = 1;
    public string ChangeContentButtonDisplaySearch
    {
        get { return _contentButtonDisplaySearch; }
        set
        {
            //if(_contentButtonDisplaySearch!= value)//this condition is not appropriate 
            //cause we have just new `string` object and this newly created
            //`string` object will be always equal to the `value` of property
            _contentButtonDisplaySearch = value;
            OnPropertyChanged("ChangeContentButtonDisplaySearch");
        }
    }

    private ICommand _changeCountCommand;
    public ICommand ChangeCountCommand
    {
        get
        {
            if (_changeCountCommand == null)
            {
                _changeCountCommand = new RelayCommand(
                p => ChangeViewModel(),
                p => true);
            }
            return _changeCountCommand;
        }
    }

    private void ChangeViewModel()
    {
        ChangeContentButtonDisplaySearch++;            
    }  
}

ViewModelBase 类:

public class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

你的xml:

<Window x:Class="SampleApplication.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:SampleApplication"
    Title="MainWindow" Height="350" Width="525">
  <Window.DataContext>
     <local:YourViewModel/>
  </Window.DataContext>
     <StackPanel>
      <Button Content="{Binding ChangeContentButtonDisplaySearch}" Command="{Binding ChangeCountCommand}"></Button>
      <TextBlock Text="{Binding ChangeContentButtonDisplaySearch}"/>        
  </StackPanel>
</Window>

或在代码后面查看:

using System;
using System.Windows;
namespace WpfApplication.DataBinding
{
        public partial class DataContextSample : Window
        {
                public DataContextSample()
                {
                        InitializeComponent();
                        this.DataContext = new YourViewModel();
                }
        }
}

【讨论】:

  • 您需要将 DataContext 设置为您的表单。由于您的视图(您的窗口)和 ViewModel 未连接,因此您的视图无法从视图模型中获取数据。请查看我的更新答案
【解决方案3】:

我的观点

 public partial class MainWindow : IView
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public void ShowIView()
        {
            this.Show();
        }

        public void ShowViewAsModal()
        {

            this.ShowDialog();
        }

        public void SetDataContext(object dataContext)
        {
            this.DataContext = dataContext;

        }

一景

 public interface IView
    {
        void ShowIView();
        void ShowViewAsModal();
        void SetDataContext(object dataContext);

    }

视图模型

public class PriceListViewModels<TViewType> : IViewModel where TViewType : IView,  new () 
    {
        private readonly IView _view;

        private readonly PriceListModel _modelPriceListModel;
        private readonly PriceModel _modelPriceModel;
        private readonly NewServInPriceListModel _modelNewServInPriceListModel;

        public ObservableCollection<PriceList> PriceListObservableCollection { get; set; }
        public ObservableCollection<Price> PriceObservableCollection { get; set; }
        public ObservableCollection<NewServInPriceList> NewServObServableCollection { get; set; }

        public RelayCommand CommandSave { get; private set; }


        // создаем объект ICollection View, для отображения и фильтрации данных в Datagrid
        public ICollectionView collview { get; private set; }

        public ObservableCollection<bool> Colec { get; set; }


        #region fields
        private string _textBoxSearch;  // переменная которая связана с TextBoxSearch во View
        private DateTime _dateTimeBeginPriceList; 
        private DateTime _dateTimeEndPriceList;
        private int _selectedIndexListBox;
        private string _textBoxComment;
        private string _contentButtonDisplaySearch;

        // переменные для выбора типов оплаты
        private bool _omsIsChecked;
        private bool _dmsIsChecked;
        private bool _budshetIsChecked;
        private bool _platnoIsChecked;

        private bool _displayRadioButton;
        private bool _searchRadionButton;

        #endregion

        #region Constructors
        public PriceListViewModels()
        {
            this._view = new TViewType();

            this._modelPriceListModel = new PriceListModel();
            this.PriceListObservableCollection = new ObservableCollection<PriceList>(this._modelPriceListModel.GetService());

            this._modelPriceModel = new PriceModel();
            this.PriceObservableCollection = new ObservableCollection<Price>(this._modelPriceModel.GetPrice());

            this._modelNewServInPriceListModel = new NewServInPriceListModel();
            this.NewServObServableCollection = new ObservableCollection<NewServInPriceList>(this._modelNewServInPriceListModel.GetNewServ());
            this.CommandSave = new RelayCommand(o => this.OKRun());


            // присваиваем collview observable collection 
            collview = (CollectionView)CollectionViewSource.GetDefaultView(NewServObServableCollection);

            // задаем начальные значения для DateTimePicker
            _dateTimeBeginPriceList = DateTime.Today.AddDays(-1);
            _dateTimeEndPriceList =  new DateTime(2016, 05, 28);

            _displayRadioButton = true;

            _contentButtonDisplaySearch = "Найти";

            this._view.SetDataContext(this);
            this._view.ShowIView();
        }

我在调试我的代码,程序的执行它并没有每个Properties ChangeContentButtonDisplaySearch

【讨论】:

  • 你的意思是它没有达到步骤_contentButtonDisplaySearch = "Найти";?那么你的代码在哪里停止。同样,您需要将上面的行更改为ChangeContentButtonDisplaySearch= "Найти";。将属性设置为触发 PropertyChangedEvent 的字段而不是字段。
  • 不,contentButtonDisplaySearch = "Найти";来了
  • 当加载的属性内容工作,但随后不工作
  • 将行改为ChangeContentButtonDisplaySearch= "Найти";。设置属性,而不是要触发 PropertyChangedEvent 的字段。
  • ChangeContentButtonDisplaySearch= "Найти";结果相同
【解决方案4】:

我写了一个简单的项目,他们的问题是一样的

型号 Persons.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Learn.MVVM.Example.Annotations;

namespace Learn.MVVM.Example.Models.Business
{
    public class Person : INotifyPropertyChanged
    {
        #region Fields

        private string _firstName;
        private string _middleName;
        private string _lastName;
        private DateTime _dateOfBirth;
        private Gender _gender;

        #endregion Fields

        #region Properties

        public string FirstName
        {
            get { return _firstName; }
            set
            {
                if (value == _firstName) return;
                _firstName = value;
                OnPropertyChanged();
            }
        }
        public string MiddleName
        {
            get { return _middleName; }
            set
            {
                if (value == _middleName) return;
                _middleName = value;
                OnPropertyChanged();
            }
        }
        public string LastName
        {
            get { return _lastName; }
            set
            {
                if (value == _lastName) return;
                _lastName = value;
                OnPropertyChanged();
            }
        }
        public DateTime DateOfBirth
        {
            get { return _dateOfBirth; }
            set
            {
                if (value.Equals(_dateOfBirth)) return;
                _dateOfBirth = value;
                OnPropertyChanged();
            }
        }
        public Gender Gender
        {
            get { return _gender; }
            set
            {
                if (value == _gender) return;
                _gender = value;
                OnPropertyChanged();
            }
        }

        #endregion Properties

        #region Constructors

        public Person()
        {

        }

        public Person(string firstName, string middleName, string lastName, DateTime dateOfBirth, Gender gender)
        {
            FirstName = firstName;
            MiddleName = middleName;
            LastName = lastName;
            DateOfBirth = dateOfBirth;
            Gender = gender;
        }

        #endregion Constructors

        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        //[NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion INotifyPropertyChanged

    }

    public enum Gender
    {
        Male,
        Female
    }
}

人物模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Learn.MVVM.Example.Models.Business;

namespace Learn.MVVM.Example.Models
{
    public class PersonModel
    {
        #region Constructors

        public PersonModel()
        {

        }

        #endregion Constructors)

        #region Methods

        public List<Person> GetPersons()
        {
            return new List<Person>
            {
                new Person("Алексей", "Алексеевич", "Алексеев", new DateTime(1980, 5, 10), Gender.Male),
                new Person("Петр", "Петрович", "Петров", new DateTime(1977, 9, 21), Gender.Male),
                new Person("Виктория", "Викторовна", "Викторова", new DateTime(1991, 1, 7), Gender.Female)
            };
        }

        #endregion Methods

    }
}

视图模型

IViewModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Learn.MVVM.Example.ViewModels
{
    public interface IViewModel
    {
    }
}

PersonsViewModel.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Learn.MVVM.Example.Common.Commands;
using Learn.MVVM.Example.Models;
using Learn.MVVM.Example.Models.Business;
using Learn.MVVM.Example.Views;

namespace Learn.MVVM.Example.ViewModels
{
    public class PersonsViewModel<TViewType> : IViewModel where TViewType : IView, new()
    {
        private readonly IView _view;
        private readonly PersonModel _model;

        public ObservableCollection<Person> Persons { get; set; }
        public RelayCommand OkCommand { get; private set; }

        private string _str;

        public PersonsViewModel()
        {
            this._view = new TViewType();
            this._model = new PersonModel();
            this.Persons = new ObservableCollection<Person>(this._model.GetPersons());
            this.OkCommand = new RelayCommand(o => this.OKRun());

            _str = "Кнопка";


            this._view.SetDataContext(this);
            this._view.ShowView();


        }


        public string Str
        {
            get { return _str; }
            set
            {
                if (_str == value)
                    return;
                _str = value;
                OnPropertyChanged("Str");

            }


        }


        public event PropertyChangedEventHandler PropertyChanged;

        //[NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }


        private void OKRun()
        {
            _str = "Change";
        }
    }
}

查看

IView.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Learn.MVVM.Example.Views
{
    public interface IView
    {
        void ShowView();
        void ShowViewAsModal();
        void SetDataContext(object dataContext);
    }
}

PersonsView.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Learn.MVVM.Example.Views
{
    /// <summary>
    /// Логика взаимодействия для PersonsView.xaml
    /// </summary>
    public partial class PersonsView: IView
    {
        public PersonsView()
        {
            InitializeComponent();
        }

        public void ShowView()
        {
            this.Show();
        }

        public void ShowViewAsModal()
        {
            this.ShowDialog();
        }

        public void SetDataContext(object dataContext)
        {
            this.DataContext = dataContext;
        }
    }
}

PersonsView.xaml

<Window x:Class="Learn.MVVM.Example.Views.PersonsView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="PersonsView" Height="300" Width="300">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="50"/>
        </Grid.RowDefinitions>

        <DataGrid ItemsSource="{Binding Persons}" />
        <Button Content="{Binding Str, UpdateSourceTrigger=PropertyChanged}" DataContext="{Binding Str}" Name="OKButton"  Command="{Binding OkCommand}" Grid.Row="1"/>
    </Grid>
</Window>

命令

RelayCommands.cs

using System;
using System.Windows.Input;

namespace Learn.MVVM.Example.Common.Commands
{
    public class RelayCommand : ICommand
    {
        private Predicate<object> canExecute;
        private Action<object> execute;

        public RelayCommand(Action<object> execute) : this(execute, DefaultCanExecute)
        {
        }

        public RelayCommand(Action<object> execute, Predicate<object> canExecute)
        {
            if (execute == null)
            {
                throw new ArgumentNullException("execute");
            }

            if (canExecute == null)
            {
                throw new ArgumentNullException("canExecute");
            }

            this.execute = execute;
            this.canExecute = canExecute;
        }

        public event EventHandler CanExecuteChanged
        {
            add
            {
                CommandManager.RequerySuggested += value;
                CanExecuteChangedInternal += value;
            }

            remove
            {
                CommandManager.RequerySuggested -= value;
                CanExecuteChangedInternal -= value;
            }
        }

        public bool CanExecute(object parameter)
        {
            return canExecute != null && canExecute(parameter);
        }

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

        private event EventHandler CanExecuteChangedInternal;

        public void OnCanExecuteChanged()
        {
            var handler = CanExecuteChangedInternal;
            if (handler != null)
            {
                handler.Invoke(this, EventArgs.Empty);
            }
        }

        public void Destroy()
        {
            canExecute = _ => false;
            execute = _ => { };
        }

        private static bool DefaultCanExecute(object parameter)
        {
            return true;
        }
    }
}

【讨论】:

    【解决方案5】:

    我找到了正确的答案

    视图模型

    我忘了添加 INotifyPropertyChanged

    public class PriceListViewModels<TViewType> : **INotifyPropertyChanged**, IViewModel where TViewType : IView,  new () 
    

    感谢所有试图提供帮助的人

    【讨论】:

      猜你喜欢
      • 2012-08-21
      • 1970-01-01
      • 2022-01-02
      • 2017-03-28
      • 2014-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多