Wpf Mvvm ComboBox

Posted by 2Fast4YouBR on Stack Overflow See other posts from Stack Overflow or by 2Fast4YouBR
Published on 2010-06-09T16:10:08Z Indexed on 2010/06/10 2:02 UTC
Read the original article Hit count: 604

Filed under:
|

Hi All,

I am new in the Wpf world, so I created a couple of views and all of them have at least one ComboBox, as I am using the MvvM pattern, I get my self re-typing all the time the same line of codes to fill the Combo and to get the SelectedItem (creating properties, privates for fill and other to get).

Is there some kind of framework that can improve this part ? or hack/trick ??? as I see too much repetitive code... maybe I am doing something wrong, take a look:

XAML:

<ComboBox name= "cbDepartments" DisplayMemberPath="DepartmentName"
                      SelectedValuePath ="PrimaryKey"
                      ItemsSource="{Binding Path=Departments}" 
                      SelectedItem="{Binding Path=DefaultBranch,Mode=TwoWay}" 
>

ViewModel:

private Department defaultBranch;
        public Department DefaultBranch
        {
            get
            {
                return this.defaultBranch;
            }

            set
            {
                if (this.defaultBranch != value)
                {
                    this.defaultBranch = value;
                    this.OnPropertyChanged("DefaultBranch");
                    this.saveChangeCommand.RaiseCanExecuteChanged();
                    this.UserMessage = string.Empty;
                }
            }
        }

private ObservableCollection<Department> departments; 
public ObservableCollection<Department> Departments
        {
            get { return this.departments; }
            set
            {
                if (this. departments!= value)
                {
                    this. departments = value;
                    this.OnPropertyChanged("Departments");
                }
            }
        }

© Stack Overflow or respective owner

Related posts about wpf

Related posts about mvvm