Last active
August 19, 2017 17:56
-
-
Save s-fernandez-v/5f44ddcd1239a0409811d1ce428808bd to your computer and use it in GitHub Desktop.
Attached property to connect a Command to a RoutedEvent 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
class EventToCommand | |
{ | |
public: | |
static const DependencyProperty* CommandProperty; | |
static ICommand* GetCommand(const DependencyObject* obj) | |
{ | |
NS_ASSERT(obj != 0); | |
auto command = obj->GetValue<Ptr<BaseComponent> >(CommandProperty); | |
return NsDynamicCast<ICommand*>(command.GetPtr()); | |
} | |
static void SetCommand(DependencyObject* obj, ICommand* value) | |
{ | |
NS_ASSERT(obj != 0); | |
auto command = NsStaticCast<BaseComponent*>(value->GetBaseObject()); | |
obj->SetValue<Ptr<BaseComponent> >(CommandProperty, command); | |
} | |
static const DependencyProperty* EventProperty; | |
static const NsChar* GetEvent(const DependencyObject* obj) | |
{ | |
return obj->GetValue<NsString>(EventProperty).c_str(); | |
} | |
static void SetEvent(DependencyObject* obj, const NsChar* value) | |
{ | |
obj->SetValue<NsString>(EventProperty, value); | |
} | |
private: | |
static void EventPropertyChangedCallback(DependencyObject* obj, | |
const DependencyPropertyChangedEventArgs& args) | |
{ | |
const NsString& eventName = *static_cast<const NsString*>(args.newValue); | |
if (!eventName.empty()) | |
{ | |
UIElement* element = NsDynamicCast<UIElement*>(obj); | |
if (element != 0) | |
{ | |
auto event = FindEvent(NsSymbol(eventName), element->GetClassType()); | |
if (event != 0) | |
{ | |
element->AddHandler(event, &EventToCommand::OnEvent); | |
} | |
} | |
} | |
} | |
static const RoutedEvent* FindEvent(NsSymbol eventName, const TypeClass* type) | |
{ | |
const TypeClass* elementType = TypeOf<UIElement>(); | |
while (type != 0) | |
{ | |
auto metadata = type->GetMetaData().Find<UIElementData>(); | |
if (metadata != 0) | |
{ | |
auto event = metadata->FindEvent(eventName); | |
if (event != 0) | |
{ | |
return event; | |
} | |
} | |
if (type == elementType) | |
{ | |
break; | |
} | |
type = type->GetBase(); | |
} | |
return 0; | |
} | |
static void OnEvent(BaseComponent* sender, const RoutedEventArgs&) | |
{ | |
UIElement* element = NsDynamicCast<UIElement*>(sender); | |
if (element != 0) | |
{ | |
ICommand* command = GetCommand(element); | |
if (command != 0 && command->CanExecute(0)) | |
{ | |
command->Execute(0); | |
} | |
} | |
} | |
NS_IMPLEMENT_INLINE_REFLECTION(EventToCommand, NoParent) | |
{ | |
NsMeta<TypeId>("EventToCommand"); | |
auto data = NsMeta<DependencyData>(TypeOf<SelfClass>()); | |
data->RegisterProperty<Ptr<BaseComponent> >(CommandProperty, "Command", | |
PropertyMetadata::Create(Ptr<BaseComponent>())); | |
data->RegisterProperty<NsString>(EventProperty, "Event", | |
PropertyMetadata::Create(NsString(), &EventToCommand::EventPropertyChangedCallback)); | |
} | |
}; | |
const DependencyProperty* EventToCommand::CommandProperty; | |
const DependencyProperty* EventToCommand::EventProperty; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment