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 any one Help me
Do the following steps:
Make sure that the collection
Categorys
is filled already. You might need to use AsycCTP, Asynchronous Programming with Async and Await or some other mechanism to first wait for the collection to be filled.Implement
INotifyPropertyChanged
in ViewModel exposing theProperty
,CurrentCategory
and raise the event of PropertyChanged from within theSetter
of theProperty
.Now you can use the same piece of code:
You Should Try This Way also.................
List c = new List
Try using
ICollectionView
andIsSynchronizedWithCurrentItem
. The CollectionView has all the functionality you need. For exampleMoveToFirst()
.Xaml:
ViewModel: