Wednesday, October 31, 2012

Listbox First Item selected in Mvvm

Listbox First Item selected in Mvvm

Hi all,

I am new to mvvm. I have a listbox in my silverlight application which is binded to a observable collection in view model i want to make the listbox with first item selected. I tired this but it doesnt work.

<ListBox Height="431" Canvas.Left="17" Canvas.Top="77" Width="215" FontSize="13" ItemsSource="{Binding Path=Categorys, Mode=TwoWay}" DataContext="{Binding}" SelectedItem="{Binding CurrentCategory, Mode=TwoWay}" ItemTemplate="{StaticResource CategoryDataTemplate}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Name="lst_category">

then i added this in mainpage load of mainpage viewmodel

CurrentCategory = Categorys[0];

can anyone help me



Answers & Comments...

Answer: 1

Can you show the implementation of CurrentCategory?  Have you implemented INotifyPropertyChanged?



Answer: 2

Yes I have implemented INotifyProperty Changed.

private m_category _CurrentCategory=null;     public m_category CurrentCategory          {              get              {                  return _CurrentCategory;              }                set              {                  if (_CurrentCategory != value)                  {                      _CurrentCategory = value;                      OnPropertyChanged("CurrentCategory");                  }              }          }  





Answer: 3

Here is what you need to do:

ViewModel.Cs

public ObservableCollection<ComboItem> ComboItems { get; set; }  private object currentItem;          public object CurrentItem        {           get { return currentItem; }           set {                 currentItem = value;                 PropertyChangedNotify("CurrentItem");           }        }        //constructor        public ViewModel()        {           ComboItems = new ObservableCollection<ComboItem>()           {  new ComboItem() { Id=1, ItemValue="First"},              new ComboItem() { Id=2, ItemValue="Second"},              new ComboItem() { Id=3, ItemValue="Third"}           };             CurrentItem = ComboItems[0];        }  
public class ComboItem : INotifyPropertyChanged     {        private int id;          public int Id        {           get { return id; }           set { id = value; }        }        private string value;          public string ItemValue        {           get { return this.value; }           set           {              this.value = value;              PropertyChangedNotify("ItemValue");           }        }          private void PropertyChangedNotify(string p)        {           if (PropertyChanged != null)           {              PropertyChanged(this, new PropertyChangedEventArgs(p));           }        }            public event PropertyChangedEventHandler PropertyChanged;     }  

ComboBox Binding.
<ComboBox Name="CategoryCombo"                  Width="100"                  Height="20"                  HorizontalAlignment="Stretch"                  VerticalAlignment="Stretch"                  DisplayMemberPath="ItemValue"                  ItemsSource="{Binding ComboItems}"                  SelectedItem="{Binding CurrentItem,                                         Mode=TwoWay}"                  SelectedValuePath="Id" />

and It works.





No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog