[英]WPF binding doesn't work with ICommand
I have a window contains table ( DataGrid
), which open new window after double click to line. 我有一个包含表的窗口(
DataGrid
),双击后将打开新窗口。 I use MouseBinding
: MouseAction="LeftDoubleClick" Command="{Binding MouseDoubleClickCommand}
我使用
MouseBinding
: MouseAction="LeftDoubleClick" Command="{Binding MouseDoubleClickCommand}
SelectedSubject
at ExecuteCurrentObjectCommand
function is correct and DataContext
looks like set at debug. ExecuteCurrentObjectCommand
函数中的SelectedSubject
是正确的, DataContext
看起来像在调试时设置的。
class ApplicationViewModel : INotifyPropertyChanged
{
private List<Subject> sub = Subject.GetSubjects();
public ObservableCollection<Subject> Subjects { get; set; }
private Subject selectedSubject;
public Subject SelectedSubject
{
get { return selectedSubject; }
set
{
selectedSubject = value;
OnPropertyChanged("selectedSubject");
}
}
public ApplicationViewModel()
{
Subjects = new ObservableCollection<Subject>(sub);
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string prop = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
private RelayCommand mouseDoubleClickCommand;
public ICommand MouseDoubleClickCommand
{
get
{
if (mouseDoubleClickCommand == null)
mouseDoubleClickCommand = new RelayCommand(ExecuteCurrentObjectCommand,
CanExecuteCurrentObjectCommand);
return mouseDoubleClickCommand;
}
}
private bool CanExecuteCurrentObjectCommand(object obj)
{
return SelectedSubject == null ? false : true;
}
private void ExecuteCurrentObjectCommand(object obj)
{
var oEW = new InfoWindow();
var vm = new InfoViewModel();
vm.SelectedObject = SelectedSubject;
oEW.DataContext = vm;
oEW.Show();
}
}
InfoViewModel
class
contains only SelectedObject
field (with get/set functions). InfoViewModel
class
仅包含SelectedObject
字段(带有get / set函数)。 InfoWindow
is inherited Window
and do only InitializeComponent()
. InfoWindow
是继承的Window
并且仅执行InitializeComponent()
。
I also create RelayCommand
class
. 我还创建了
RelayCommand
class
。
class RelayCommand : ICommand
{
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
public RelayCommand(Action<object> execute, Predicate<object> canExecute = null)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute.Invoke(parameter);
}
public event EventHandler CanExecuteChanged
{
...
}
public void Execute(object parameter)
{
_execute.Invoke(parameter);
}
}
InfoWindow.xaml
code InfoWindow.xaml
代码
<Window.Resources>
<local:Subject x:Key="MSource" />
</Window.Resources>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<Label Content="Name:"/>
<Label Content="{Binding Name, Source={StaticResource MSource}}"></Label>
</StackPanel>
New window is created but binding doesn't work. 已创建新窗口,但绑定无效。 What's problem?
什么问题?
public class Subject : INotifyPropertyChanged
{
private int _id;
public int Id
{
get => _id;
set
{
_id = value;
OnPropertyChanged("Id");
}
}
private string _name;
public string Name
{
get => _name;
set
{
_name = value;
OnPropertyChanged("Name");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName]string prop = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
InfoViewModel
object was assigned to DataContext
incorrect. InfoViewModel
对象分配给DataContext
错误。 In xaml I deleted Resourses and changed binding. 在xaml中,我删除了资源,并更改了绑定。
<Label Content="{Binding Name}"></Label>
Correct assignment: 正确分配:
oEW.DataContext = vm.SelectedObject;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.