tags:

views:

93

answers:

2

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");
                }
            }
        }
A: 

Most of what you have looks pretty standard. There are a few things you can cut down:

  • It looks like you aren't using SelectedValue so you can remove SelectedValuePath
  • SelectedItem is TwoWay by default so you can remove the Mode=TwoWay from that binding
  • For the departments property you should be able to remove the setter entirely and instead add and remove items in the existing collection. This can also help to avoid issues with ItemsSource bindings not getting correct notifications - INotifyCollectionChanged works more consistently that INotifyPropertyChanged on the collection property. Departments could collapse down to:

public ObservableCollection<Department> Departments { get; private set; }

John Bowen
Thanks John, I didn´t know about the INotifyColletion, I will digg to find out, cheers.
2Fast4YouBR
ObservableCollection implements INotifyCollectionChanged for you so as long as you're using it you get the notifications for free when the set of items in the collection is modified.
John Bowen
A: 

As for making a custom control for the combobox with departments - that is really easy in WPF:

<ComboBox DisplayMemberPath="DepartmentName" x:Class="...DepartmentComboBox"
          SelectedValuePath ="PrimaryKey"
          ItemsSource="{Binding Path=Departments}" 
          SelectedItem="{Binding Path=DefaultBranch,Mode=TwoWay}"/>

And Code-behind:

public partial class DepartmentComboBox
{
    public DepartmentComboBox()
    {
        InitializeComponent();
    }
}
Goblin