How do I format the current item in a BarComboBox?

Bars for WPF Forum

Posted 1 year ago by Steve Nicholson
Version: 23.1.2
Avatar

I have a BarComboBox that contains a list of DateTime values. I want to format the dates as "Jun 14, 2023". This works for the dates in the drop-down list, but the currently selected date shows up as DateTime's default string format "6/14/2023 12:00". How do I specify the format for the current item? Here's my XAML code:

<bars:BarComboBox
    Label="Check dates"
    ItemsSource="{Binding CheckDates}"
    SelectedIndex="{Binding SelectedCheckDateIndex}"
    ItemStringFormat="MMM d, yyyy"/>

Comments (3)

Posted 1 year ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Steve,

The BarComboBox control ignores the ItemStringFormat property.  Even the native WPF controls ignore that property when an ItemTemplate/ItemTemplateSelector is defined like with BarComboBox.

Even if you don't use the MVVM pattern in your application, BarComboBox is one of the rare instances where the items source should be based on view models to work properly in all scenarios.  BarComboBox is technically a gallery control so we can provide advanced functionality in the dropdown and support various overflow scenarios, so that's why view models are required.  You don't have to use our companion MVVM library, but it does provide the base BarGalleryItemViewModel<TValue> class that you can derive from for you own view model. Using that base class, you can override the CoerceLabel method to control how the label for the item is formatted.

public class DateTimeBarGalleryItemViewModel : BarGalleryItemViewModel<DateTime> {

    public DateTimeBarGalleryItemViewModel(DateTime value) : base(value) { }

    protected override string CoerceLabel() {
        return base.Value.ToString("MMM d, yyyy");
    }

}

If you didn't want to use the MVVM library, you would need to define your own view model for the BarComboBox items and define your own ItemTemplate for how to display each item.


Actipro Software Support

Posted 1 year ago by Steve Nicholson
Avatar

Thanks for the reply. I guess I need to spend more time with the Sample Browser because I don't know what to put in a XAML file to make it use the C# code you supplied. I've been a programmer for a long time, but this is the first project where I have to create a Windows UI from scratch. I'm still trying to gain traction.

Answer - Posted 1 year ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Steve,

Our galleries have some advanced, complex features and need to be driven off of view models as their ItemsSource.  At a bare minimum, you could:

  • Add a reference to our Bars MVVM library.
  • Add the C# class in our previous reply to your project.
  • Create a collection of the DateTimeBarGalleryItemViewModel instances you want to be in the BarComboBox drop-down.
  • Examine our "/BarSamples/QuickStart/ComboBoxAndEditors/SampleXamlControl.xaml" file since it shows usage of BarComboBox from XAML.  This assumes you are creating your bar controls in XAML instead of via MVVM.  Copy one of those examples to your project.
  • Note how the BarComboBox instances there bind something to their ItemsSource.  That's what you'd do, bind to the collection from the previous step.
  • Adjust the other properties from the BarComboBox example as appropriate.  But you'd want to keep the ItemContainerStyle and ItemTemplateSelector the same as in the sample.

The Gallery documentation topic has a lot of information on gallery items, and is a good resource to consult.


Actipro Software Support

The latest build of this product (v24.1.2) was released 3 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.