0

I'm using the following XAML to create a simple list of DayOfMonth values:

<ComboBox SelectedValue="{Binding DayOfMonth}" SelectedValuePath="/">
  <ComboBoxItem>
    <sys:Int32>1</sys:Int32>
  </ComboBoxItem>
  <ComboBoxItem>
    <sys:Int32>2</sys:Int32>
  </ComboBoxItem>
  ...
  ...
</ComboBox>

The problem is that it doesn't select correct item (in fact it doesn't select any) in the ComboBox when the underlying ViewModel has one assigned to DayOfMonth property (property type is Int32). I have tried it without <sys:Int32> as well, i.e. directly using <ComboBoxItem>1</ComboBoxItem> syntax, that doesn't work either.

Note: I'm looking for XAML-only solution.

Edit

Though it is not of much value, here is the property I'm binding my ComboBox to:

private int mDayOfMonth;
public int DayOfMonth 
{
    get { return mDayOfMonth; }
    set 
    {
        if (mDayOfMonth != value) 
        {
             mDayOfMonth = value;
             if (PropertyChanged != null) 
                PropertyChanged(this, new PropertyChangedEventArgs("DayOfMonth"));
        }
    }
}
dotNET
  • 33,414
  • 24
  • 162
  • 251
  • So what does your code for the viewmodel and for the code behind of the view look like? – slugster Dec 29 '14 at 05:07
  • You want a XAML only solution, but you have a ViewModel class. If so, why aren't you binding the ComboBox ItemsSource property to a property in the viewmodel (for the days in the month). – Walt Ritscher Dec 29 '14 at 05:11
  • @slugster: The only relevant part in the ViewModel is that `Int32` property that this ComboBox is binding to. – dotNET Dec 29 '14 at 05:13
  • @WaltRitscher: That's how MVVM generally works, right? XAML-only solution means that I don't want to use a for-loop to fill items in the ComboBox, but rather add them declaratively. – dotNET Dec 29 '14 at 05:15
  • In MVVM you have a property of type ObservableCollection on your ViewModel (for example (DaysInCurrentMonth). Fill the ObservableCollection in some manner and then bind the ComboBox.ItemsSource to the property. – Walt Ritscher Dec 29 '14 at 05:20
  • @WaltRitscher: Rightly said. However my intent here is discover a design-time way of adding items to the ComboBox that I could use binding with. – dotNET Dec 29 '14 at 05:25
  • If that's the case, then you should look into using design-time data. http://stackoverflow.com/questions/1889966/what-approaches-are-available-to-dummy-design-time-data-in-wpf – Walt Ritscher Dec 29 '14 at 07:40
  • @WaltRitscher: Perhaps I used wrong word there. I do not want to use design-time data, those only show in the designer. What I was after was to be able to add items to the ComboBox manually through XAML, and then bind the ComboBox's `SelectedValue` property to an `Int32` field of the `DataContext`. @octavioccl's answer already achieves that. Thanks for your interest and time. – dotNET Dec 29 '14 at 08:11

1 Answers1

0

You don't need to declare the interger values inside ComboBoxItems. Something like this should work:

<ComboBox SelectedValue="{Binding DayOfMonth}">
  <sys:Int32>1</sys:Int32>>
  <sys:Int32>2</sys:Int32>
  ...
</ComboBox>
ocuenca
  • 38,548
  • 11
  • 89
  • 102
  • This somehow works. Had to remove `SelectedValuePath="/"` clause from ComboBox though. Thanks a bunch. – dotNET Dec 29 '14 at 06:25
  • The int values are still wrapped in an implicit ComboBoxItem – Walt Ritscher Dec 29 '14 at 07:38
  • @WaltRitscher: Correct, but if I instead use `` myself in XAML, it will get nested in a double-``. That's my guess though. Not sure if WPF is intelligent enough not to use implicit wrapping when the item type is a `ComboBoxItem` itself. – dotNET Dec 29 '14 at 08:09
  • It won't get wrapped in another ComboBoxItem. The ItemsContainerGenerator http://msdn.microsoft.com/en-us/library/system.windows.controls.itemcontainergenerator(v=vs.110).aspx is responsible for determining whether an implicit or explicit ComboBoxItem is used. You should download XAMLSpy, it shows you the WPF visual tree. That way you can see what XAML is actually rendered at runtime. – Walt Ritscher Dec 29 '14 at 08:35