Topic Author
Posts: 5
Joined: 05 May 2023, 06:03

Triggering event from xaml -> c++?

29 May 2023, 23:27

I feel like this is a simple thing but I can't seem to get these events to fire.

I've got a collection of RealmUI_Tell that are bound as the ItemsSource of the tab control. The content bindings work (Sender, StaffTitle, and Text)

But I'm trying to get an event to fire when the Close button is clicked, but can't seem to get this event to fire. I tried using the NsEvent macro to associate OnCloseClicked with the evt_close RoutedEventHandler, and then I register a lambda with evt_close when I create the RealmUI_Tell, however this event doesn't get fired, and there is no error or otherwise any output from Noesis when registering this stuff or clicking the button - so I'm not sure where the problem is in hooking this up properly.

Some advice would be greatly appreciated!
struct RealmUI_Tell : Noesis::BaseComponent
	Noesis::String sender;
	Noesis::String staff_title;
	Noesis::Ptr<Noesis::TextBlock> text = Noesis::MakePtr<Noesis::TextBlock>();

	Noesis::RoutedEventHandler evt_close;

	const Noesis::String& GetSender() const
		return sender;
	const Noesis::String& GetStaffTitle() const
		return staff_title;

	Noesis::TextBlock* GetText() const
		return text;

	NS_IMPLEMENT_INLINE_REFLECTION(RealmUI_Tell, Noesis::BaseComponent)
		NsProp("Sender", &RealmUI_Tell::GetSender);
		NsProp("StaffTitle", &RealmUI_Tell::GetStaffTitle);
		NsProp("Text", &RealmUI_Tell::GetText);

		NsEvent("OnCloseClick", &RealmUI_Tell::evt_close);
                    <TabControl TabStripPlacement="Left" x:Name="TellsTabs">
                                <Label Content="{Binding Sender}" />
                                        <RowDefinition Height="25pt" />
                                        <RowDefinition Height="*" />
                                    <Grid Grid.Row="0">
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="Auto" />
                                        <Label Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Center" Content="{Binding StaffTitle}" />
                                        <StackPanel Grid.Column="1" HorizontalAlignment="Right" Orientation="Horizontal">
                                            <Button Click="{Binding OnCloseClick}">Close</Button>
                                    <ContentControl Grid.Row="1" Content="{Binding Text}" />
User avatar
Site Admin
Posts: 3033
Joined: 22 Dec 2011, 19:20

Re: Triggering event from xaml -> c++?

31 May 2023, 18:08

If I try to set a binding in the Button Click event I get this error in Noesis: "Unknown property 'Button.Click', or object to assign is incompatible".

Button Click event can only be set to a function from the code-behind class, not bindings.

Anyway, if you are trying to execute some code in your viewmodel you should take a look at commands. Basically you expose a command in your viewmodel and then bind to that object in the Command property of the Button:
<Button Command="{Binding CloseCommand}">Close</Button>
Topic Author
Posts: 5
Joined: 05 May 2023, 06:03

Re: Triggering event from xaml -> c++?

31 May 2023, 19:18

Aha. I had overlooked 'commands', this seems to work fine for triggering events like this. Thanks!

Who is online

Users browsing this forum: No registered users and 12 guests