Wanderer
Topic Author
Posts: 84
Joined: 08 May 2017, 18:36

(C++) Select item in ListBox with Button inside ListBox, how to?

17 Jun 2017, 11:09

I have ListBox and all items have button with custom control, inspired with UserControlTutorial. I can easily select item and make codebehind actions, but I lost one day without success, how to get IndexOf from button with custom user control, when button is clicked? I mean, when button from item indexed as 4 in ListBox is clicked, it get that index. I looked in to xaml and C# solutions, but I am unable to recreate it with C++ and Noesis. Functions are different in Noesis as from WPF.

I was thinking about add some value to the Button when is created, but ListBox is dynamic, and any item can be deleted and then added in to list what breake correct indexing for Buttons. (if second item is deleted from three, then second button will have index 3, but it have index 2 (from item in ListBox) and if new item is added, there will be two buttons with index 3).
 
Wanderer
Topic Author
Posts: 84
Joined: 08 May 2017, 18:36

Re: (C++) Select item in ListBox with Button inside ListBox, how to?

18 Jun 2017, 00:46

I get new idea, and it is looks like working. But there is another problem.
My idea is, when button is clicked in ListBox, user can change item name. Instead ItemTemplate="{DynamicResource TaskTemplate}"I change TaskTemplate to Container which is part of data model. Now I dont need select item. It is looks like everything work, but I got new problem. After change Item name, it is not showed on Item. If I display name on Console, it is changed, but not inside ListBox, that means, item name is changed inside, but not visualy from gui. I tried Binding with TwoWay mode, but without success.
 
Wanderer
Topic Author
Posts: 84
Joined: 08 May 2017, 18:36

Re: (C++) Select item in ListBox with Button inside ListBox, how to?

18 Jun 2017, 14:56

Finally I resolved it.
I used
NS_IMPLEMENT_INLINE_REFLECTION(Container, UserControl)
	{
		NsMeta<TypeId>("Container");
		//NsProp("SourceN", &Container::mSource);
		
		NsProp("Midle", &Container::mMidle);
		Ptr<UIElementData> data = NsMeta<UIElementData>(TypeOf<SelfClass>());
		data->RegisterProperty<NsString>(TextProperty, "SourceN", 
			FrameworkPropertyMetadata::Create(NsString("asd"), FrameworkOptions_None));
	}
and
static const DependencyProperty* TextProperty;
But I have problem create name from NsString mSource which is in Class.
 
User avatar
sfernandez
Site Admin
Posts: 1138
Joined: 22 Dec 2011, 19:20

Re: (C++) Select item in ListBox with Button inside ListBox, how to?

19 Jun 2017, 20:53

I have ListBox and all items have button with custom control, inspired with UserControlTutorial. I can easily select item and make codebehind actions, but I lost one day without success, how to get IndexOf from button with custom user control, when button is clicked? I mean, when button from item indexed as 4 in ListBox is clicked, it get that index. I looked in to xaml and C# solutions, but I am unable to recreate it with C++ and Noesis. Functions are different in Noesis as from WPF.

I was thinking about add some value to the Button when is created, but ListBox is dynamic, and any item can be deleted and then added in to list what breake correct indexing for Buttons. (if second item is deleted from three, then second button will have index 3, but it have index 2 (from item in ListBox) and if new item is added, there will be two buttons with index 3).
Assuming the button is inside the item data template, you can probably obtain the item by calling GetDataContext() on the button. Then, if you know the ListBox instance you are included, you can convert the item to an index by using the ItemContainerGenerator:
void OnButtonClicked(BaseComponent* sender, const RoutedEventArgs& e)
{
    Button* button = (Button*)sender;
    BaseComponent* item = button->GetDataContext();
    ItemContainerGenerator* generator = mListBox->GetItemContainerGenerator();
    DependencyObject* container = generator->ContainerFromItem(item);
    NsInt index = generator->IndexFromContainer(container);
    // ...
}
I get new idea, and it is looks like working. But there is another problem.
My idea is, when button is clicked in ListBox, user can change item name. Instead ItemTemplate="{DynamicResource TaskTemplate}"I change TaskTemplate to Container which is part of data model. Now I dont need select item. It is looks like everything work, but I got new problem. After change Item name, it is not showed on Item. If I display name on Console, it is changed, but not inside ListBox, that means, item name is changed inside, but not visualy from gui. I tried Binding with TwoWay mode, but without success.
Finally I resolved it.
I used ...
But I have problem create name from NsString mSource which is in Class.
You don't need to create dependency properties to bind data in the view that is automatically updated. Probably you missed to implement the INotifyPropertyChanged interface and raise the PropertyChanged event after modifying the view model name property.
Please take a look at the sample I posted in viewtopic.php?f=3&t=1115, maybe it can help you understand how to organize your application data and views better.
 
Wanderer
Topic Author
Posts: 84
Joined: 08 May 2017, 18:36

Re: (C++) Select item in ListBox with Button inside ListBox, how to?

21 Jun 2017, 13:47

Thanks for code. I was aware of INotifyPropertyChanged, but I was unable to implement in to my class. Only after your example I was able to do. On data binding tutorial or other tutorials are different examples and your example is much simpler and understandable.

But it is not clear, there you wrote I don't need dependency properties, but when are good to use dependency properties? And what properties means in Noesis?
 
User avatar
sfernandez
Site Admin
Posts: 1138
Joined: 22 Dec 2011, 19:20

Re: (C++) Select item in ListBox with Button inside ListBox, how to?

22 Jun 2017, 16:39

DependencyProperties are usually defined in controls or any other DependencyObjects. If you want a property to have bindings or be animated by a Storyboard, you have to use a DependencyProperty.

But when you are defining a ViewModel, the easiest way to expose simple properties is via the class reflection, and use INotifyPropertyChange if you want to notify of property changes so UI can be automatically updated.

Who is online

Users browsing this forum: No registered users and 3 guests