WPF绑定不适用于ICommand

时间:2018-05-18 21:14:02

标签: c# wpf xaml data-binding

我有一个包含表格(DataGrid)的窗口,双击线后会打开新窗口。我使用MouseBindingMouseAction="LeftDoubleClick" Command="{Binding MouseDoubleClickCommand}

SelectedSubject函数的{p> 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字段(带有get / set函数)。 InfoWindowWindow继承的,只会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));
    }
}

1 个答案:

答案 0 :(得分:-1)

if add_dress_form.is_valid(): model_instance = add_dress_form.save(commit=False) model_instance.created_by = current_user model_instance.save() return redirect('home') 对象已分配给 if add_dress_form.is_valid(): model_instance = add_dress_form.save(commit=False) model_instance.created_by = current_user model_instance.save() return redirect('home') 不正确。在xaml中,我删除了资源并更改了绑定。

prevalence

正确分配:

// max of selected vaccine
var ex = [{"name": "a", "prevalence": "NaN"},
	{"name": "a", "prevalence": "5"},
	{"name": "b", "prevalence": "0"},
	{"name": "a", "prevalence": "NaN"},
	{"name": "c", "prevalence": "100"},
	{"name": "c", "prevalence": "20"},
	{"name": "a", "prevalence": "3"}];
console.log('ex:', ex);

var maxPrevalence = d3.max(ex, function(d) {
	if(!isNaN(d.prevalence)) {
		return d.prevalence;
	}
});
console.log('\nmaxPrevalence:', maxPrevalence);