realesmedia
Topic Author
Posts: 85
Joined: 18 May 2016, 10:26

[C++] Binding command to ListBox item select event

28 Feb 2017, 08:25

How to bind to select event of listbox for item?

XAML View file:
<Listbox Selected="{Binding OnSelected}" ItemsSource="{Binding TheList2}" ItemTemplate="{StaticResource ListTemplate2}"/>
C++ code behind:
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.
 
User avatar
ai_enabled
Posts: 221
Joined: 18 Jul 2013, 05:28
Contact:

Re: [C++] Binding command to ListBox item select event

28 Feb 2017, 09:40

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.
AtomicTorch Studio Pte. Ltd. http://atomictorch.com
 
realesmedia
Topic Author
Posts: 85
Joined: 18 May 2016, 10:26

Re: [C++] Binding command to ListBox item select event

28 Feb 2017, 10:03

Could you provide code-behind sample?
 
User avatar
ai_enabled
Posts: 221
Joined: 18 Jul 2013, 05:28
Contact:

Re: [C++] Binding command to ListBox item select event

28 Feb 2017, 10:34

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.
AtomicTorch Studio Pte. Ltd. http://atomictorch.com
 
realesmedia
Topic Author
Posts: 85
Joined: 18 May 2016, 10:26

Re: [C++] Binding command to ListBox item select event

28 Feb 2017, 11:20

Ok, I got it, thanks.
 
User avatar
sfernandez
Site Admin
Posts: 1911
Joined: 22 Dec 2011, 19:20

Re: [C++] Binding command to ListBox item select event

02 Mar 2017, 04:14

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:
<Listbox SelectedItem="{Binding TheSelectedItem}" ItemsSource="{Binding TheList2}" ItemTemplate="{StaticResource ListTemplate2}"/>
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
Topic Author
Posts: 85
Joined: 18 May 2016, 10:26

Re: [C++] Binding command to ListBox item select event

07 Mar 2017, 10:18

@sfernandez, your solution solves the problem.
 
User avatar
sfernandez
Site Admin
Posts: 1911
Joined: 22 Dec 2011, 19:20

Re: [C++] Binding command to ListBox item select event

08 Mar 2017, 10:09

Good to know, thanks for the update.

Who is online

Users browsing this forum: No registered users and 0 guests