简体   繁体   中英

WPF binding doesn't work with ICommand

I have a window contains table ( DataGrid ), which open new window after double click to line. I use MouseBinding : MouseAction="LeftDoubleClick" Command="{Binding MouseDoubleClickCommand}

SelectedSubject at ExecuteCurrentObjectCommand function is correct and DataContext looks like set at debug.

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). InfoWindow is inherited Window and do only InitializeComponent() .

I also create 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

<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. In xaml I deleted Resourses and changed binding.

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

Correct assignment:

oEW.DataContext = vm.SelectedObject;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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