- realesmedia
- Posts: 85
- Joined:
[C++] Register multiple events for collection
How to register one event to each element of collection if last one changes dynamicaly?
How to register event on each addition and removing of elements?
Model, XAML:
View, XAML:
Use case, for example:
1. user click the button;
2. new checkbox appending to listbox as item;
3. checkbox have registered event OnCheck();
Second use case:
1. user select listbox element which is checkbox;
2. user click the button;
3. listbox have the element removed and no event connected to it;
How to register event on each addition and removing of elements?
Model, XAML:
Code: Select all
<Grid.Resources>
<DataModel x:Name="dataModel" />
<DataTemplate x:Key="ListTemplate">
<CheckBox x:Name="CheckListBoxItem" Content="{Binding content}" Tag="{Binding value}"/>
</DataTemplate>
</Grid.Resources>
Code: Select all
<ListBox x:Name="CheckListBox" ItemsSource="{Binding TheList}" DataContext="{StaticResource dataModel}" ItemTemplate="{StaticResource ListTemplate}"/>
1. user click the button;
2. new checkbox appending to listbox as item;
3. checkbox have registered event OnCheck();
Second use case:
1. user select listbox element which is checkbox;
2. user click the button;
3. listbox have the element removed and no event connected to it;
Last edited by realesmedia on 09 Jan 2017, 11:52, edited 1 time in total.
- ai_enabled
- Posts: 231
- Joined:
- Contact:
Re: [C++] Register multiple events for collection
Hi!
AFAIK, it's not possible with the item templates. But you can bind the IsChecked property of the checkbox:This way you don't need to use XAML events. Also it works for buttons in the same way - instead of subscribing to the click event you can simply bind the button Command property.
BTW, we're making our second big game with NoesisGUI, we have over hundred of controls and don't using any XAML events (and rarely use any events in codebehind). Only databinding. MVVM pattern is super useful and very easy to learn. We expect our modding community to learn it with our tutorials (not yet done) and modify/extend our game UI. It's even easier as we have UI live reloading feature (so it will rebuild UI and code nearly instantly).
Regards!
AFAIK, it's not possible with the item templates. But you can bind the IsChecked property of the checkbox:
Code: Select all
<CheckBox Content="{Binding content}" Tag="{Binding value}" IsChecked="{Binding IsCheckedPropertyInItemDataContext}" />
BTW, we're making our second big game with NoesisGUI, we have over hundred of controls and don't using any XAML events (and rarely use any events in codebehind). Only databinding. MVVM pattern is super useful and very easy to learn. We expect our modding community to learn it with our tutorials (not yet done) and modify/extend our game UI. It's even easier as we have UI live reloading feature (so it will rebuild UI and code nearly instantly).
Regards!
AtomicTorch Studio Pte. Ltd. http://atomictorch.com
- realesmedia
- Posts: 85
- Joined:
Re: [C++] Register multiple events for collection
Can you give C++ code example?But you can bind the IsChecked property of the checkbox
- ai_enabled
- Posts: 231
- Joined:
- Contact:
Re: [C++] Register multiple events for collection
I'm using C# SDK so I cannot write a quick example in C++...
But you can check v1.3 Doc/Gui.Core.CommandsTutorial.html - there is a good tutorial how to create a view model with the command properties (for buttons).
It works in the same way to checkboxes, but you don't need commands - you need to create a simple Boolean field in the view model and bind to it.
Then you need to simply make an observable collection of these view models. Perhaps you already done this as I see you have binding of ItemsSource="{Binding TheList}" and binding of Content and Tag properties of the checkbox in the item template - so you need to add to this view model class a new Boolean property and bind it in XAML as I showed above. In this Boolean property set method (which will be invoked by NoesisGUI automatically when you click on the checkbox) you can do any logic you wanted to do in the checkbox click event handler.
Regards!
But you can check v1.3 Doc/Gui.Core.CommandsTutorial.html - there is a good tutorial how to create a view model with the command properties (for buttons).
It works in the same way to checkboxes, but you don't need commands - you need to create a simple Boolean field in the view model and bind to it.
Then you need to simply make an observable collection of these view models. Perhaps you already done this as I see you have binding of ItemsSource="{Binding TheList}" and binding of Content and Tag properties of the checkbox in the item template - so you need to add to this view model class a new Boolean property and bind it in XAML as I showed above. In this Boolean property set method (which will be invoked by NoesisGUI automatically when you click on the checkbox) you can do any logic you wanted to do in the checkbox click event handler.
Regards!
AtomicTorch Studio Pte. Ltd. http://atomictorch.com
-
sfernandez
Site Admin
- Posts: 2984
- Joined:
Re: [C++] Register multiple events for collection
As ai_enabled suggested you can bind to the IsChecked property, and execute any code when set to true:Can you give C++ code example?But you can bind the IsChecked property of the checkbox
Code: Select all
<Grid.Resources>
<DataModel x:Name="dataModel" />
<DataTemplate x:Key="ListTemplate">
<CheckBox x:Name="CheckListBoxItem" Content="{Binding content}" Tag="{Binding value}"
IsChecked="{Binding checked}"/>
</DataTemplate>
</Grid.Resources>
Code: Select all
class ItemVM: public Noesis::BaseComponent
{
public:
// ...
bool GetChecked() const { return _checked; }
void SetChecked(bool checked)
{
if (_checked != checked)
{
_checked = checked;
if (_checked)
{
// do whatever you want here...
}
}
}
private:
bool _checked;
NS_IMPLEMENT_INLINE_REFLECTION(ItemVM, Noesis::BaseComponent)
{
NsProp("checked", &ItemVM::GetChecked, &ItemVM::SetChecked);
}
};
- realesmedia
- Posts: 85
- Joined:
Re: [C++] Register multiple events for collection
Thanks for replies.
When I put IsCheck event into data template it doesn't work (get/set functions doesn't call). Without data template works great.
When I put IsCheck event into data template it doesn't work (get/set functions doesn't call). Without data template works great.
Code: Select all
<Grid.Resources>
<DataModel x:Name="dataModel" />
<DataTemplate x:Key="ListTemplate">
<CheckBox x:Name="CheckListBoxItem" Content="{Binding content}" Tag="{Binding value}" IsChecked="{Binding OnChecked}"/>
</DataTemplate>
</Grid.Resources>
<StackPanel VerticalAlignment="Center" Width="auto">
<ListBox x:Name="CheckListBox" ItemsSource="{Binding TheList}" DataContext="{StaticResource dataModel}" ItemTemplate="{StaticResource ListTemplate}"/>
</StackPanel>
Code: Select all
public:
void SetOnChecked(bool);
bool GetOnChecked() const {
return mChecked;
}
private:
NS_IMPLEMENT_INLINE_REFLECTION(DataModel, BaseComponent) {
NsProp("OnChecked", &DataModel::GetOnChecked, &DataModel::SetOnChecked);
}
bool mChecked;
- ai_enabled
- Posts: 231
- Joined:
- Contact:
Re: [C++] Register multiple events for collection
@realesmedia, it seems you've put this C++ code into the view model containing the list ("TheList") property - it will not work. You need to put it into the the view model class used for items of this list (the view model containing the "content" and "value" properties). Your data template binds to the list elements view models.
AtomicTorch Studio Pte. Ltd. http://atomictorch.com
- realesmedia
- Posts: 85
- Joined:
Re: [C++] Register multiple events for collection
I'll try to separate logic. Will report on try.@realesmedia, it seems you've put this C++ code into the view model containing the list ("TheList") property - it will not work. You need to put it into the the view model class used for items of this list (the view model containing the "content" and "value" properties). Your data template binds to the list elements view models.
- realesmedia
- Posts: 85
- Joined:
Re: [C++] Register multiple events for collection
You are right. Separated logic for view model works.
-
sfernandez
Site Admin
- Posts: 2984
- Joined:
Re: [C++] Register multiple events for collection
Yes, the "onChecked" property should be added to the view model of the item itself, not the view model that exposes the list of items. Maybe it was not clear enough in my code snippet, sorry for the confusion.
It works fine now then? could we mark this as solved?
It works fine now then? could we mark this as solved?
Who is online
Users browsing this forum: Google [Bot], Semrush [Bot] and 93 guests