BarComboBox selected item DataTemplate

Bars for Avalonia Forum

Posted 2 days 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 (1)

Posted 1 days 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

Add Comment

Please log in to a validated account to post comments.