Showing posts with label silverlight. Show all posts
Showing posts with label silverlight. Show all posts

Tuesday, 16 November 2010

ComboBox binding in Silverlight

Using a combobox in silverlight is really easy but it's a little more tricky when you want to bind data to the combobox, it tends to bind ok but setting the SelectedItem doesn't get set.

Therefore I used the notify property changed to update the UI instead. Heres how you can achieve the same:
XAML:
<UserControl x:Class="TestSLApp.MainPage"
xmlns:this="clr-namespace:TestSLApp"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.Resources>
<this:ViewModel x:Key="VM" />
</Grid.Resources>
<ComboBox Width="100" Height="50"
ItemsSource="{Binding MYLIST, Source={StaticResource VM}}"
SelectedItem="{Binding MySelectedItem, Mode=TwoWay, Source={StaticResource VM}}"
>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding SomeText}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
</UserControl>

Code Behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace TestSLApp
{
public partial class MainPage : UserControl
{
private ViewModel VIEWMODEL
{
get
{
return LayoutRoot.Resources["VM"] as ViewModel;
}
}

public MainPage()
{
InitializeComponent();
VIEWMODEL.MYLIST = new List()
{
new DropDownItem() { ID = 1, SomeText="Text1"},
new DropDownItem() { ID = 2, SomeText="Text2"},
new DropDownItem() { ID = 3, SomeText="Text3"},
new DropDownItem() { ID = 4, SomeText="Text4"},
new DropDownItem() { ID = 5, SomeText="Text5"},
new DropDownItem() { ID = 6, SomeText="Text6"}
};
VIEWMODEL.MySelectedItem = VIEWMODEL.MYLIST.FirstOrDefault(o => o.ID == 5);
}
}


public class ViewModel : INotifyPropertyChanged
{
private List _list = null;
public List MYLIST
{
get
{
return _list;
}
set
{
if (_list == value)
return;
_list = value;
NotifyPropertyChanged("MYLIST");
}
}

private DropDownItem _mySelectedItem = null;
public DropDownItem MySelectedItem
{
get
{
return _mySelectedItem;
}
set
{
if (_mySelectedItem == value)
return;
_mySelectedItem = value;
NotifyPropertyChanged("MySelectedItem");
}
}


public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(null, new PropertyChangedEventArgs(propertyName));
}
}

public class DropDownItem
{
public int ID { get; set; }
public string SomeText { get; set; }
}
}


I actually put the ViewModel class in a seperate document but have kept it in the same code here for easy reference.