Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
860
How to set the datetime format for the SelectedItem in a xamComboEditor
posted

I have a list of DateTime items for a xamComboEditor, I am trying to make the combobox show the selecteditem in a 'MM-dd-yyyy' format, but unsuccessful. when the app first starts, it always show the selecteditem as '10/11/2015 12:00 AM'. I was able to set the dropdown list to be in the 'MM-dd-yyyy' format, but once the combobox losses focus, it will show the date in the 'dd/MM/yyyy HH:mm AM' format.

The xaml:

<Window x:Class="WpfApplication7.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication7"
xmlns:igWpf="http://schemas.infragistics.com/xaml/wpf"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>

    <igWpf:XamComboEditor 
          ItemsSource ="{Binding DateList}"
          SelectedItem="{Binding SelectedDate}">
          <igWpf:XamComboEditor.ComboBoxStyle>
                  <Style TargetType="ComboBox">
                          <Setter Property="ItemTemplate">
                                  <Setter.Value>
                                       <DataTemplate>
                                               <TextBlock Text="{Binding StringFormat='MM-dd-yyyy'}"/>
                                        </DataTemplate>
                                   </Setter.Value>
                             </Setter>
                      </Style>
                  </igWpf:XamComboEditor.ComboBoxStyle>
      </igWpf:XamComboEditor>

      <TextBox Grid.Column="1"/>
</Grid>
</Window>

Codes behind:

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

DateList = new ObservableCollection<DateTime>()
{
new DateTime(2005,09,15),
new DateTime(2015,10,15),
new DateTime(2015,11,15)
};
SelectedDate = DateList[1];
DataContext = this;
}


private ObservableCollection<DateTime> _DateList;
public ObservableCollection<DateTime> DateList
{
get { return _DateList; }
set
{
if (value != _DateList)
{
_DateList = value;
//RaisePropertyChanged();
}
}
}


private DateTime _SelectedDate;
public DateTime SelectedDate
{
get { return _SelectedDate; }
set
{
if (value != _SelectedDate)
{
_SelectedDate = value;
//RaisePropertyChanged();
}
}
}

}

I also have tried the following:

<igWpf:XamComboEditor.ComboBoxStyle>
    <Style TargetType="ComboBox">
        <Setter Property="IsEditable">
            <Setter.Value>False</Setter.Value>
        </Setter>
        <Setter Property="ItemStringFormat">
              <Setter.Value>MM-dd-yyyy</Setter.Value>
         </Setter>>
     </Style>
</igWpf:XamComboEditor.ComboBoxStyle>

but getting the same result.

Thanks.

  • 34810
    Verified Answer
    Offline posted

    Hello yingwu,

    Thank you for your post.

    I have been investigating into this, and the ComboBoxStyle and the properties related to the ComboBox won't really help you here, as the XamComboEditor uses a combo box only when it is in edit mode or the drop down is open. When the XamComboEditor does not have focus, these properties don't have much of a visual effect.

    I would recommend that you utilize the ValueToDisplayTextConverter property of the XamComboEditor to show the dates in the format that you are looking to show them in. This property takes an IValueConverter. This converter's Convert method will fire when the control exits edit mode and the current value that is in the editor will be available in the converter via the object Value parameter of that method. You can modify that and return a string value of your choosing to modify the display text in the XamComboEditor to show your DateTime object in the format that you would like to see.

    Please let me know if you have any other questions or concerns on this matter.

    Sincerely,
    Andrew
    Associate Developer
    Infragistics Inc.
    www.infragistics.com/support