- realesmedia
- Posts: 85
- Joined:
[C++] Binding command to ListBox item select event
How to bind to select event of listbox for item?
XAML View file:
C++ code behind:
XAML View file:
Code: Select all
<Listbox Selected="{Binding OnSelected}" ItemsSource="{Binding TheList2}" ItemTemplate="{StaticResource ListTemplate2}"/>
Code: Select all
class ViewModel_imports : public BaseComponent{
public:
ViewModel_imports() {
mListBoxItems = *new ObservableCollection<Item_model>;
cmd_on_selected = *new DelegateCommand(MakeDelegate(this, &ViewModel_imports::OnSelected));
}
Noesis::Ptr<ObservableCollection<Item_model> > mListBoxItems;
void OnSelected(BaseComponent* sender) {}
private:
Noesis::Ptr<DelegateCommand> cmd_on_selected ;
DelegateCommand* get_cmd_on_selected() const { return cmd_on_selected .GetPtr(); };
NS_IMPLEMENT_INLINE_REFLECTION(ViewModel_imports, BaseComponent) {
NsMeta<TypeId>("ViewModel2");
NsProp("TheList2", &ViewModel_imports::mListBoxItems);
NsProp("OnSelected", &ViewModel_imports::get_cmd_on_selected);
}
};
Last edited by realesmedia on 28 Feb 2017, 11:20, edited 1 time in total.
- ai_enabled
- Posts: 231
- Joined:
- Contact:
Re: [C++] Binding command to ListBox item select event
You cannot bind to events.
You can bind to only dependency/attached properties.
In case of ListBox, you can bind to SelectedItem property. Similarly to how you bind to ItemsSource.
And then in your ViewModel when SelectedItem property change you could execute the logic responsible for reacting on selection change.
You can bind to only dependency/attached properties.
In case of ListBox, you can bind to SelectedItem property. Similarly to how you bind to ItemsSource.
And then in your ViewModel when SelectedItem property change you could execute the logic responsible for reacting on selection change.
AtomicTorch Studio Pte. Ltd. http://atomictorch.com
- realesmedia
- Posts: 85
- Joined:
Re: [C++] Binding command to ListBox item select event
Could you provide code-behind sample?
- ai_enabled
- Posts: 231
- Joined:
- Contact:
Re: [C++] Binding command to ListBox item select event
Unfortunately, no - I'm using C# only.
But you already knew how to data bind any property from ViewModel, right?
You simply need to do any logic you want in the setter of that property in ViewModel.
This way when you select any item in the ListBox, the property SelectedItem of the ListBox will be updated and via data binding the according setter will be executed in your ViewModel.
But you already knew how to data bind any property from ViewModel, right?
You simply need to do any logic you want in the setter of that property in ViewModel.
This way when you select any item in the ListBox, the property SelectedItem of the ListBox will be updated and via data binding the according setter will be executed in your ViewModel.
AtomicTorch Studio Pte. Ltd. http://atomictorch.com
- realesmedia
- Posts: 85
- Joined:
Re: [C++] Binding command to ListBox item select event
Ok, I got it, thanks.
-
sfernandez
Site Admin
- Posts: 3154
- Joined:
Re: [C++] Binding command to ListBox item select event
Thanks ai_enabled for your collaboration.
About the code you were asking for, in case any other faces the same problem, you need to write something like this:
About the code you were asking for, in case any other faces the same problem, you need to write something like this:
Code: Select all
<Listbox SelectedItem="{Binding TheSelectedItem}" ItemsSource="{Binding TheList2}" ItemTemplate="{StaticResource ListTemplate2}"/>
Code: Select all
class ViewModel_imports : public BaseComponent {
public:
// ...
BaseComponent* GetTheSelectedItem() const {
return mTheSelectedItem.GetPtr();
}
void SetTheSelectedItem(BaseComponent* item) {
if (mTheSelectedItem != item) {
mTheSelectedItem.Reset(item);
OnSelectedItemChanged();
}
}
void OnSelectedItemChanged() {
// do whatever you need on selected item change
}
//...
NS_IMPLEMENT_INLINE_REFLECTION(ViewModel_imports, BaseComponent) {
NsMeta<TypeId>("ViewModel2");
NsProp("TheList2", &ViewModel_imports::mListBoxItems);
NsProp("TheSelectedItem", &ViewModel_imports::GetTheSelectedItem, &ViewModel_imports::SetTheSelectedItem);
}
};
- realesmedia
- Posts: 85
- Joined:
Re: [C++] Binding command to ListBox item select event
@sfernandez, your solution solves the problem.
-
sfernandez
Site Admin
- Posts: 3154
- Joined:
Re: [C++] Binding command to ListBox item select event
Good to know, thanks for the update.
Who is online
Users browsing this forum: No registered users and 2 guests