1

Resolved - The theme was interfering with the display

This is my first experience with WPF so there may be an obvious answer to this.

I'm trying to display a Month selection combobox where the month names are displayed, and when a selection is made the integer value is captured.

XAML

<ComboBox Margin="5" IsEditable="False" 
         IsEnabled="{Binding IsCompanyFileUploadPeriodEnabled}" 
         ItemsSource="{Binding StartMonths}" 
         DisplayMemberPath="Key" 
         SelectedValuePath="Value" 
         SelectedValue="{Binding SelectedStartMonthID}" 
         Width="50"></ComboBox>

Edit: The ViewModel extends the Galasoft MvvmLight ViewModelBase, which provides the RaisePropertyChanged method.

ViewModel

Dictionary<string, int> _startMonths;
public Dictionary<string, int> StartMonths
{
    get
    {
        if (_startMonths == null)
        {
            _startMonths = new Dictionary<string, int>();
            for (int i = 1; i < 13; i++)
            {
                _startMonths.Add(System.Globalization.DateTimeFormatInfo.CurrentInfo.GetMonthName(i),
                    i);
            }
        }
        return _startMonths;
    }
}

int _selectedStartMonthID;
public int SelectedStartMonthID
{
    get
    {
        return _selectedStartMonthID;
    }
    set
    {
        _selectedStartMonthID = value;
        RaisePropertyChanged(() => SelectedStartMonthID);
    }
}

But for some reason when I run the app the combobox is displaying as

  • [January, 1]
  • [February, 2]
  • etc

Does anyone know why it might be ignoring the DisplayMemberPath instruction? The SelectedValuePath setting seems to be working fine when an element is selected.

SuicideSheep
  • 5,260
  • 19
  • 64
  • 117
Sam Banks
  • 85
  • 13
  • It's always advisable trying FallbackValue - 'WrongBinding' if you'll still see wrong values - it must be something else, not bindings. – user3455395 Apr 01 '14 at 23:18
  • I can understand why you would want to have a fall back in the case of dynamic data, but this is a static list of month names & values. It should bind every time... – Sam Banks Apr 01 '14 at 23:22
  • First, I would strongly suggest that you take the initialization of _startMonths out of the get and put it in the constructor of your class. Otherwise you will get a new dictionary built every time that property is retrieved. – FodderZone Apr 01 '14 at 23:23
  • There's no real need for FallbackValue, it's just the way to see if you binding is broken up./ The fact that you see items tellms me that there's nothing fundamentally wrong - likely the twiseted bindings. You sure you use Dictionary, not a list of tuples?? – user3455395 Apr 01 '14 at 23:25
  • Where is RaisePropertyChanged defined? – FodderZone Apr 01 '14 at 23:26
  • 1
    @FodderZone - It will only initialize on the first load. If it's been called before, the private variable will not be null and that code wont be called. – Sam Banks Apr 01 '14 at 23:26
  • 1
    Are you sure you aren't applying any styles not located here? I copied your code exactly and only the month names are shown in the ComboBox. – Oren Hizkiya Apr 01 '14 at 23:27
  • @FodderZone - I've edited the question. The ViewModels extend the Galasoft MVVM baseViewModel class which provides the RaisePropertyChanged method. – Sam Banks Apr 01 '14 at 23:30
  • 2
    Not sure why you are getting ,1 ,2. But I would have the integer as the key. – paparazzo Apr 01 '14 at 23:31
  • @Oren - That annoying. There aren't any styles on this page. The App is using the WhistlerBlue.xaml theme. Maybe I'll try turning that off to see if it has any effect. – Sam Banks Apr 01 '14 at 23:33
  • Yes, try that and then see. – Oren Hizkiya Apr 01 '14 at 23:35
  • I agree with @Blam I created a sample project with your code and it works as is. I've never used Galasoft MVVM class. I've always implemented INotifyPropertyChanged. http://stackoverflow.com/a/1316417/1706610 – FodderZone Apr 01 '14 at 23:37
  • Thanks for your feedback people. I'll try changing some things to see if that affects the behaviour. – Sam Banks Apr 01 '14 at 23:44

2 Answers2

2

ComboBox DisplayMemberPath binding is broken by Themes BureauBlue and WhistlerBlue

http://wpf.codeplex.com/workitem/10129

Sam Banks
  • 85
  • 13
  • Great, seems still pretty broken in Windows 10 VS 2017. I dont think i have bureaublue anywhere. Any idea what to do with this? – zaitsman Jul 16 '17 at 11:58
  • Sorry, I haven't touched WPF since this question was raised. I would suggested checking whether changing the theme helps any. Maybe the suggested fix posted in the link would help? – Sam Banks Jul 26 '17 at 04:10
0

Take a look at StringFormat.

MSDN StringFormat

Wrongway
  • 47
  • 6