我有这段代码
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时,Button内容发生了变化,但是当我尝试改变时没有任何反应
答案 0 :(得分:4)
直接修改属性而不是其值。如果修改变量set
将不会被触发,并且OnPropertyChange();
事件将不会被触发。
尝试直接通过属性分配值:
private void OKRun()
{
ChangeContentButtonDisplaySearch = "ChangeContent";
}
<Button/>
xaml
UpdateSourceTrigger
应设置<Button Content="{Binding ChangeContentButtonDisplaySearch, UpdateSourceTrigger=PropertyChanged}"/>
属性:
DataContext
另外,尝试添加OnStartup();
有很多方法可以这样做。您可以尝试通过覆盖App.xaml.cs
文件中的protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow _window = new MainWindow();
ClassWhereIsYourPropertyStored _vM = new ClassWhereIsYourPropertyStored();
_window.DataContext = _vM;
_window.Show();
}
事件来添加它。
funcExit()
答案 1 :(得分:1)
我真的很感兴趣为什么Content
控件的Button
属性实际上没有更新。我找到了原因。原因string
类型是不可变的,如果条件if(_contentButtonDisplaySearch!= value)
不合适,因为我们只有新的string
对象,这个新创建的string
对象将始终等于{{1属性:
您的观看模型:
value
ViewModelBase类:
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++;
}
}
你的xaml:
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));
}
}
或在视图后面的代码:
<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>
答案 2 :(得分:0)
我的观点
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);
}
ViewModel
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();
}
我调试我的代码,执行programm它并不是每个属性ChangeContentButtonDisplaySearch
答案 3 :(得分:0)
我写了一个简单的项目,类比他们的问题是一样的
模型 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
}
}
<强> PersonModel 强>
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
}
}
的ViewModels
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;
}
}
}
答案 4 :(得分:0)
我找到了正确的答案
ViewModel
我忘了添加INotifyPropertyChanged
public class PriceListViewModels<TViewType> : **INotifyPropertyChanged**, IViewModel where TViewType : IView, new ()
感谢所有试图提供帮助的人