Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Missing SelectedValue and SelectedValuePath properties in ComboBox
4 replies. Latest Post by mahendranvs on June 18, 2009.
(0)
sd@sercl...
Member
6 points
25 Posts
10-20-2008 4:52 AM |
Hi,
I'm very surprise to not find the SelectedValue and SelectedValuePath properties in the new ComboBox control. Why this missing and how to do to databind it ?
HarshBar...
Star
9908 points
1,719 Posts
10-20-2008 6:25 AM |
you can bind data like this.
//Page.Xaml
<StackPanel x:Name="LayoutRoot" Background="White"> <ComboBox x:Name="mycomboBox" ItemsSource="{Binding}" SelectionChanged="mycomboBox_SelectionChanged" Height="50"></ComboBox>
<ComboBox x:Name="Listbox" Visibility="Visible" Height="50">
<ComboBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Str}" /> <TextBlock Text="Details" /> </StackPanel>
</DataTemplate> </ComboBox.ItemTemplate> </ComboBox>
</StackPanel>
//Page.xaml.cs
public Page() { InitializeComponent(); List<Name> name = new List<Name>(); name.Add(new Name { Str = "Harsh" }); name.Add(new Name { Str = "HarshBardhan" }); name.Add(new Name { Str = "HarshBardhanHarsh" }); name.Add(new Name { Str = "Harsh" });
var Query = from c1 in name // where c1.Str == "Harsh" select new Name { Str = c1.Str };
Listbox.SelectedIndex = -1; Listbox.ItemsSource = Query;mycomboBox.DataContext=Query;mycomboBox.DisplayMemberPath="Str";
} public class Name { private string str;
public string Str { get { return str; } set { str = value; } } }
private void mycomboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {Name a =mycomboBox.SelectedItem as Name;
} } }
spiderma...
220 points
276 Posts
11-13-2008 4:16 AM |
Hi, Harsh.I have one problem with your sample.
I just add a button and in button's click event I write like below:
private void BtnOK_Click(object sender, RoutedEventArgs e) { ObservableCollection<Name> name = new ObservableCollection<Name>(); name.Add(new Name { Str = "Harsh" }); name.Add(new Name { Str = "HarshBardhan" }); name.Add(new Name { Str = "HarshBardhanHarsh" }); name.Add(new Name { Str = "Harsh" }); var Query = from c1 in name //where c1.Str == "Harsh" select new Name { Str = c1.Str }; mycomboBox.DataContext = Query; mycomboBox.DisplayMemberPath = "Str"; mycomboBox.SelectedIndex = 0; }
I changed the selectedIndex from -1 to 0, and when i change the selectedItem of mycomboBox and click the button, error occurs. I know there is something wrong with selectedIndex, but i really want the selectedIndex to be 0.
any suggustions?
Thanks
ZeekJones
4 points
5 Posts
12-02-2008 7:00 PM |
http://silverlight.net/forums/p/53720/139118.aspx#139118
http://www.engineserver.com/silverlightcombobox/
mahendranvs
8 points
6 Posts
06-18-2009 8:59 AM |
While setting the selected item of silverlight combo box, there is an issue. I have tried updatelayout and finally i have inherited and created a new combo control. Then overided the method PrepareContainerForItemOverride and call base method with try catch which works fine. See my post below for sample code.
http://silverlight.net/forums/p/103044/234997.aspx#234997