简体   繁体   English

WPF绑定不适用于ICommand

[英]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} 我使用MouseBindingMouseAction="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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM