BarComboBox selected item DataTemplate

Bars for Avalonia Forum

Posted 2 months ago by Matt
Version: 25.2.0
Avatar

Hi,

New user and enjoying the product so far!

I am porting a set of WPF applications over to Avalonia and am having some issues with styling the selected item in a BarComboBox. I know that this was not straightforward in WPF days but was hoping things might be simpler now..

What I am trying to achieve is actually very simple (for now), which is to show a drop-down list of colour options and have that colour shown in the selected item display. What I have tried so far: Simply using a static list of a ColorViewModel class with a single Color member, with a DataTemplate configured for the ColorViewModel data type in app.axaml which just has a border and a background. The list of ColorViewModel is bound to the ItemsSource of the BarComboBox and colours displayed in the drop-down as you might expect, but with nothing showing in the selected item.

Next I took a closer look at the samples project and tried wrapping the ColorViewModel in a class derived from BarGalleryItemViewModel<ColorViewModel>, and provided a static list of objects of this class to the BarComboBox. One change from before: the selected item started to show the ToString value of the selected ColorViewModel, so it does appear that the object is bound to the selected area but the data template is not being used.

I have also tried explicity setting the ItemTemplate directly, and using an ItemTemplateSelector - which appears to only be called for the drop-down.

Is what I am doing possible please and if so how to approach?

thanks,

Matt

  

Comments (3)

Answer - Posted 2 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Matt,

If you are just looking for a basic non-editable drop-down list of colors, it might be easiest to do what we did with the Text Highlight color and Font color in the main Bars demo where you have a split button (or could easily use a popup button instead) and have a color gallery one that.  Those examples show how we update the button's icon to render the color used.

Or alternatively, if you do want to use BarComboBox, you can do that as well.  BarComboBox is a gallery itself (which is why you need gallery items with it) and is intended mostly for text display in the selection box, but it can show an inline icon too.  Our "Combobox and Editors" sample has a "Categorized combobox usage" example where one of the entries there shows a BarComboBox used as a color picker.  That's a good example to study if you want to use BarComboBox.  You're probably just missing the "TextMemberBinding" to get the selection box to show the text.  Then the "InlineIconMemberBinding" can be used to display an inline icon, in this case, the selected color.


Actipro Software Support

Posted 2 months ago by Matt
Avatar

Hi,

Thanks do much for the quick and comprehensive answer. I guess I was just curious if I was missing something obvious but the idea that the BarCombo isn't really designed for this makes sense. I did get it working in our old WPF app with a ComboBox and kind of got it going with binding the main control background with your control but your suggestion to use a popup and a gallery item is a much better one.

I now have a much nicer colour picker and a single icon button that reflects the icon/colour chosen, and conforming to the way that these controls are supposed to be used.

Once more question while I am here: I am using the MVVM approach for the menu bar, so creating either a built-in or custom (derived) vm for each ribbon menu component. For some of these components - e.g. this colour gallery - I want to use basically the identical component in a separate dialog as well. Is there an MVVM way to do this or does the axaml need to wire up all the bindings to the custom BarPopupButtonViewModel that I have created? I have a tried a few things, including creating just a content control with a binding to the vm, but although this does create the right control it doesn't populate menu items etc.

thanks!

Matt

[Modified 2 months ago]

Posted 2 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Matt,

For an MVVM implementation, you would need an IItemContainerTemplateSelector in place to watch for your custom viewmodel type and return a template that initializes the color gallery with all its properties set directly or via bindings.  Our MVVM Support documentation topic covers the concepts of how MVVM works.  The container template selector is important because if implemented correctly, it will recurse down the control hierarchy and be able to fill out menu items, etc.

That being said, you really need a root control that knows how to use the IItemContainerTemplateSelector.  Ribbon, BarMainMenu, StandaloneToolBar, etc. are examples of those kinds of root controls.  The IItemContainerTemplateSelector is used to make "containers" for the children of them, but it can't style the root control itself.

If you have your color gallery just completely standalone somewhere, then you'd have to configure its properties and bindings in XAML.  You could set its BarControlService.ItemContainerTemplateSelector attached property to your IItemContainerTemplateSelector instance and that should allow the menu items to be created using the container template selector.

I hope that points you in the right direction.  If not, feel free to follow up with some more details and we can assist further.


Actipro Software Support

Add Comment

Please log in to a validated account to post comments.