Page 1 of 1

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

Posted: 28 Feb 2017, 08:25
by realesmedia
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);
  }
};

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

Posted: 28 Feb 2017, 09:40
by ai_enabled
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.

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

Posted: 28 Feb 2017, 10:03
by realesmedia
Could you provide code-behind sample?

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

Posted: 28 Feb 2017, 10:34
by ai_enabled
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.

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

Posted: 28 Feb 2017, 11:20
by realesmedia
Ok, I got it, thanks.

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

Posted: 02 Mar 2017, 04:14
by sfernandez
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);
  }
};

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

Posted: 07 Mar 2017, 10:18
by realesmedia
@sfernandez, your solution solves the problem.

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

Posted: 08 Mar 2017, 10:09
by sfernandez
Good to know, thanks for the update.