【发布时间】:2018-07-24 10:24:33
【问题描述】:
我创建了一个简单的示例来说明我的问题。我不能操纵另一个类的属性。我创建了两个类,一个有一个命令和一个属性。第二个具有相同的命令,但引用了第一个类中的属性。
具有属性和命令的类按预期工作。另一个没有,但我不明白为什么。我对此很陌生,我显然在概念上遗漏了一些东西……非常感谢您的解释。
<Window x:Class="TestProperties.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestProperties"
xmlns:mvvm="clr-namespace:TestProperties.MVVM"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Background="Black">
<Window.Resources>
<mvvm:Class1 x:Key="Class1"/>
<mvvm:Class2 x:Key="Class2"/>
</Window.Resources>
<StackPanel DataContext="{Binding Mode=OneWay, Source={StaticResource Class1}}">
<TextBox Height="33" Width="100" Margin="0 33 0 0"
Text="{Binding MyProperty, UpdateSourceTrigger=PropertyChanged}"/>
<Button Height="33" Width="100" Margin="33" Content="Class 1 - change value"
Command="{Binding ChangeValue, Mode=OneWay, Source={StaticResource Class1}}"/>
<Button Height="33" Width="100" Margin="33" Content="Class 2 - change value"
Command="{Binding ChangeValue, Mode=OneWay, Source={StaticResource Class2}}"/>
</StackPanel>
namespace TestProperties.MVVM
{
class Class1 : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
Debug.WriteLine($"Class1 NOTIFY PROPERTY CHANGED! {info.ToString()}");
}
}
private int myVar;
public int MyProperty
{
get { return myVar; }
set { myVar = value; NotifyPropertyChanged("MyProperty"); }
}
private ICommand _ChangeValue;
public ICommand ChangeValue
{
get
{
if (_ChangeValue == null)
{
_ChangeValue = new RelayCommand<object> CanExecute_ChangeValue);
}
return _ChangeValue;
}
}
public bool CanExecute_ChangeValue(object parameter)
{
return true;
}
public void Execute_ChangeValue(object parameter)
{
Console.WriteLine($"Execute_ChangeValue");
MyProperty = 5;
Console.WriteLine($"c1.MyProperty: {MyProperty}");
}
}
class Class2
{
private ICommand _ChangeValue;
public ICommand ChangeValue
{
get
{
if (_ChangeValue == null)
{
_ChangeValue = new RelayCommand<object>(Execute_ChangeValue, CanExecute_ChangeValue);
}
return _ChangeValue;
}
}
public bool CanExecute_ChangeValue(object parameter)
{
return true;
}
public void Execute_ChangeValue(object parameter)
{
Console.WriteLine($"Execute_ChangeValue");
Class1 c1 = new Class1();
c1.MyProperty = 5;
Console.WriteLine($"c1.MyProperty: {c1.MyProperty }");
}
}
public class RelayCommand<T> : ICommand
{
#region Fields
readonly Action<T> _execute = null;
readonly Predicate<T> _canExecute = null;
#endregion // Fields
#region Constructors
public RelayCommand(Action<T> execute) : this(execute, null)
{
}
public RelayCommand(Action<T> execute, Predicate<T> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
#endregion // Constructors
#region ICommand Members
//[DebuggerStepThrough]
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute((T)parameter);
}
public event EventHandler CanExecuteChanged
{
add
{ CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute((T)parameter);
}
#endregion // ICommand Members
}
【问题讨论】:
标签: c# wpf class properties