Search code examples
c#wpfmvvmtelerik-grid

List in dropdownbox in grid does not return value


I have an enum

enum SendDays
{
    Maandag = 1,
    Dinsdag,
    Vandaag= 99
}

And a Class

public struct DayListModel
{
    public int Id;
    public string DayName;
}

I fill a list with days like this

private void Filldays()
{
   foreach(int i in Enum.getValues(typeof(SendDays)))
   {
        DayListModel day =new DaylistModel()
        {
            Id = i,
            Dayname = Enum.GetName(typeof(SendDays), i)
         };
         DayList.Add(day);
   }

When I use this is in A telerik RadGridView in a radcombobox like

<telerik:RadComboBox ItemsSource="{Bindning DayList}" DisplayMemberPath="DayName" SelectedValue="{Bindning DefaultSendDay}" SelectedValuePath="Id"/>

Whenever I change the selecteditem this is not passed.

Any suggestions?

Jeroen


Solution

  • Here is my advice, and how I got it to work:

    1. Like @mm8 said, you should make the DayListModel a class not a struct, and turn the fields into properties. If you don't use properties, the DisplayMemberPath doesn't work. Some stuff doesn't work with structs (like using == to compare) so I would go with a class in this case.

    2. Are your objects observable in your view model? I don't know telerik, but you should be calling some sort of PropertyChanged method so the UI can update. If you use a framework like MVVM light it has ObservableObjects and RaisePropertyChanged methods.

    Here is a working example:

    MainWindowVM.cs

    using GalaSoft.MvvmLight;
    using System;
    using System.Collections.Generic;
    
    namespace WpfApp1
    {
        public class MainWindowVM : ObservableObject
        { 
            private List<DayListModel> _DayList;
            public List<DayListModel> DayList
            {
                get { return _DayList; }
                set
                {
                    if (value != _DayList)
                    {
                        _DayList = value;
                        RaisePropertyChanged();
                    }
                }
            }
    
    
            private DayListModel _DefaultSendDay;
            public DayListModel DefaultSendDay
            {
                get { return _DefaultSendDay; }
                set
                {
                    if (value != _DefaultSendDay)
                    {
                        _DefaultSendDay = value;
                        RaisePropertyChanged();
                    }
                }
            }
    
            public MainWindowVM()
            {
                DayList = new List<DayListModel>();
                foreach (int i in Enum.GetValues(typeof(SendDays)))
                {
                    DayListModel day = new DayListModel()
                    {
                        Id = i,
                        DayName = Enum.GetName(typeof(SendDays), i)
                    };
                    DayList.Add(day);
                }
                DayList = new List<DayListModel>(DayList);
            }
        }
    
        public enum SendDays
        {
            Maandag = 1,
            Dinsdag,
            Vandaag = 99
        }
    
        public class DayListModel
        {
            public int Id { get; set; }
            public string DayName { get; set; }
        }
    }
    
    
    

    MainWindow.xaml

    <Window x:Class="WpfApp1.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:WpfApp1"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
        <Window.DataContext>
            <local:MainWindowVM />
        </Window.DataContext>
    
        <Grid >
            <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                <ComboBox  ItemsSource="{Binding DayList}" SelectedItem="{Binding DefaultSendDay}" DisplayMemberPath="DayName" >
                </ComboBox>
                <TextBlock Text="{Binding DefaultSendDay.DayName}"/>
            </StackPanel>
        </Grid>
    </Window>
    

    Screenshot:

    Screenshot of a combobox and textblock displaying the selectd value