gpt4 book ai didi

c# - WPF绑定(bind)不适用于ICommand

转载 作者:太空宇宙 更新时间:2023-11-03 16:59:18 27 4
gpt4 key购买 nike

我有一个包含表(DataGrid)的窗口,双击该行即可打开新窗口。我使用MouseBindingMouseAction="LeftDoubleClick" Command="{Binding MouseDoubleClickCommand}

SelectedSubject函数中的ExecuteCurrentObjectCommand是正确的,并且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仅包含 SelectedObject字段(具有获取/设置功能)。 InfoWindow是继承的 Window,并且仅执行 InitializeComponent()

我还创建了 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代码

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


已创建新窗口,但绑定无效。什么问题?

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对象分配给DataContext不正确。在xaml中,我删除了资源并更改了绑定。

<Label Content="{Binding Name}"></Label>

正确分配:
oEW.DataContext = vm.SelectedObject;

关于c# - WPF绑定(bind)不适用于ICommand,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50419358/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com