Created
August 19, 2017 18:10
-
-
Save s-fernandez-v/26a8f12a0b5f0230f170a27cd98988b8 to your computer and use it in GitHub Desktop.
Custom MarkupExtension to connect a RoutedEvent to a Command in NoesisGUI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct EventBindingExtension: public BaseComponent, public IMarkupExtension | |
{ | |
EventBindingExtension() { } | |
const NsChar* GetCommandName() const { return commandName.c_str(); } | |
void SetCommandName(const NsChar* name) { commandName = name; } | |
Ptr<BaseComponent> ProvideValue(const void* context) override | |
{ | |
auto provider = (const XamlReaderProvider*)context; | |
auto element = NsDynamicCast<UIElement*>(provider->GetTargetObject()); | |
if (element != 0) | |
{ | |
auto eventId = provider->GetPropertyName(); | |
auto type = element->GetClassType(); | |
// look in UIElementData for the routed event | |
auto event = FindRoutedEvent(type, eventId); | |
if (event != 0) | |
{ | |
// TODO: connect event with a delegate that could resolve | |
// commandName from element DataContext | |
//element->AddHandler(event, ...); | |
} | |
} | |
return nullptr; | |
} | |
NS_IMPLEMENT_INTERFACE_FIXUP | |
private: | |
// ... | |
NsString commandName; | |
NS_IMPLEMENT_INLINE_REFLECTION(EventBindingExtension, BaseComponent) | |
{ | |
NsMeta<TypeId>("EventBinding"); | |
NsMeta<ContentPropertyMetaData>("CommandName"); | |
NsImpl<IMarkupExtension>(); | |
NsProp("CommandName", &EventBindingExtension::GetCommandName, | |
&EventBindingExtension::SetCommandName); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment