- realesmedia
- Posts: 85
- Joined:
[C++] Using commands
Command function doesn't call. Can't find solution.
XAML:
Source code:
XAML:
Code: Select all
<Button x:Name="button1" Content="open project" Command="{Binding ButtonClick}"/>
Code: Select all
class DelegateCommand : public BaseCommand {
public:
typedef Delegate<void(BaseComponent*)> Action;
DelegateCommand(const Action& action) : BaseCommand(NsSymbol::Null(), 0), _action(action) {}
NsBool CanExecute(BaseComponent* param) const {
return true;
}
void Execute(BaseComponent* param) const {
_action(param);
}
private:
Action _action;
NS_IMPLEMENT_INLINE_REFLECTION(DelegateCommand, BaseCommand) {
NsMeta<TypeId>("DelegateCommand");
}
};
Code: Select all
class ProjectSelect : public BaseComponent {
public:
ProjectSelect() {
mCommand = *new DelegateCommand(MakeDelegate(this, &ProjectSelect::ButtonClick));
}
void ButtonClick(BaseComponent* sender) {
// <<== Never access this point !!! =======
}
DelegateCommand* GetButtonClickCommand() const {
return mCommand.GetPtr();
}
private:
Ptr<DelegateCommand> mCommand;
NS_IMPLEMENT_INLINE_REFLECTION(ProjectSelect, BaseComponent) {
NsMeta<TypeId>("ProjectSelect");
NsProp("ButtonClick", &ProjectSelect::GetButtonClickCommand);
}
};
Last edited by realesmedia on 27 Jan 2017, 13:12, edited 1 time in total.
-
sfernandez
Site Admin
- Posts: 3154
- Joined:
Re: [C++] Using commands
Hi,
I've tested these classes and when a ProjectSelect instance is set as DataContext of the Button or one of its ancestors, then it works as expected: ButtonClick method is called when clicking on the Button.
You should check that your Button has the correct DataContext (the one who exposes the ButtonClick property).
Could you please verify that?
I've tested these classes and when a ProjectSelect instance is set as DataContext of the Button or one of its ancestors, then it works as expected: ButtonClick method is called when clicking on the Button.
You should check that your Button has the correct DataContext (the one who exposes the ButtonClick property).
Could you please verify that?
- realesmedia
- Posts: 85
- Joined:
Re: [C++] Using commands
Thanks. I didn't specify DataContext property in XAML. Problem is solved.You should check that your Button has the correct DataContext (the one who exposes the ButtonClick property).
Could you please verify that?
Re: [C++] Using commands
Thanks for the feedback!
Who is online
Users browsing this forum: Google [Bot] and 0 guests