BarPopupButton Binding ItemsSource

Bars for Avalonia Forum

Posted 16 days ago by Guillaume CHAFFAROD - Developper, Clever-Age
Version: 25.2.0
Avatar

Hello Actipro team and users.

I'm currently trying this great library but I'm running into a problem and don't know if i do not use the BarPopupButton as expected or if this is an issue.

I want to bind this control ItemsSource property to a collection defined in a model. The collection contains some objects that have the "Name" property.

The collection is observable and implements IList.

The binding seems to perform as if I set the Itememplate property, I can get all items.

The problem is that it seems the binding is lost. my datatemplate is for example a simple button specified like this:

<Button Content="{Binding Name}" /> 

The name does not display in the button content.

I dont want overload everything with a specific view model, because I will need to do this on many other components and I don't want to handle all CollectionChanged events on each collection.

What's the way to achieve what I need please ?

Thank you very much.

NB: I'm actually testing possibilities with your framework and if it meets our needs, we will buy some licenses.

Comments (3)

Posted 16 days ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

Thank you for evaluating our Bars for Avalonia library.  All of the Bars controls can work with either XAML definitions or MVVM.  I'll focus on MVVM for the rest of the response since you mentioned data binding.  Our MVVM design, once some initial configuration is in place, makes it very simple to manage large applications completely through viewmodels.

It would be helpful to know more about your intended usage scenario.  Such as are you building an entire ribbon or just making a standalone popup button here outside of a ribbon?

For BarPopupButton specifically, it is an ItemsControl that has a popup menu and the "containers" generated for each viewmodel item is a BarMenuItem by default.  However other child menu controls like separators and menu galleries can also be generated for viewmodel items.

The process for all this is described in the MVVM Support documentation topic.  Whereas a lot of ItemsControls expect to create a single "container" type for their items, and have an ItemTemplate that lets you select the "content" for a generated "container" controls, we go with a different more powerful concept called item container template selectors that is described in that topic.  Item container template selectors let you choose what child control should be used as a "container" for each ItemsControl's viewmodel item.  It's a mechanism that requires a little bit of initial setup but once done, you can populate an entire ribbon or toolbar hierarchy easily through viewmodels.

Our Bars MVVM Library that is heavily used in our samples shows an implementation of this concept.  You can use that directly or use your own viewmodels with similar concepts instead.  That library has an exhaustive viewmodel implementation for all control types supported by Bars.  You can see it fully demoed in our main Bars Document Editor (MVVM) demo, where the entire ribbon UI control hierarchy is built from a hierarchy of simple viewmodels of types from that library.

Populating the items of a popup button with that library would involve use of the item container template selector and using a collection of BarButtonViewModel, BarSeparatorViewModel, etc. instances.

As mentioned above, you can use your own viewmodels instead of ours if you wish.  You'd just need to configure a custom item container template selector (like our MVVM Library's BarControlTemplateSelector class) and set up the DataTemplates (like this for BarButtonViewModel) that selector would use to bind your viewmodels to UI controls used as containers.

Before you get into making a custom MVVM Library kind of implementation though, it's probably best to look at our samples and even try using our prebuilt MVVM Library to test concepts out first.


Actipro Software Support

Posted 13 days ago by Guillaume CHAFFAROD - Developper, Clever-Age
Avatar

Hi and thank you for your reply.

The solution came this week-end. I now use a ControlTheme in the "ItemContainerTheme" property, but I needed to set the BasedOn property on the control theme (BasedOn="{StaticResource {x:Type actipro:BarMenuItem}}").

Your library looks really great and we really think we will take soon some licenses.

Best regards,

Guillaume

Posted 13 days ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Guillaume,

That's great to hear, we look forward to having you as a customer!


Actipro Software Support

Add Comment

Please log in to a validated account to post comments.