Skip to main content
Home Forums Silverlight Programming Report a Silverlight Bug ListBox does not display SelectedItem the first time when ItemsSource are value types
4 replies. Latest Post by Jonathan Shen – MSFT on July 2, 2009.
(0)
HiPerFreak
Member
0 points
19 Posts
06-25-2009 1:34 AM |
Hi,
I have a strange problem. I have a simple ListBox that I bind to a PresentationModel class:
1 <ListBox x:Name="listBox" 2 ItemsSource="{Binding Months}" 3 SelectedItem="{Binding SelectedMonth}" />
In the constructor of my presentation model I set the 'Months' to a List<int> and set 'SelectedMonth' to 6 and assign the DataContext of the view:
1 public DemoPresentationModel(Page view) 2 { 3 this.months = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; 4 this.selectedMonth = 6; 5 view.Model = this; 6 }
But if the view is displayed, no item is selected! But if I read the SelectedItem property the correct value (6) is returned. So the ListBox has a discrepancy between its property values and the display state.
If I change the SelectedMonth property after the view is displayed (e.g. in a button click event handler it works fine).
I made a simple solution that demonstrates the problem. You can download it here (click on the "Right-Click here ..." link at the top).
Please tell me if there are any workaround available for this problem, because this is quite an essential requirement for the type of application I am developing.
Thank you- Daniel
06-25-2009 2:11 AM |
It may have to do something with the default template. In a plain new solution the SelectedItem binding works at least with reference types. But if I use the implicit sytle manager to set the WhistlerBlue style on a parent element, it stops working with reference types too!
Jonathan...
All-Star
24939 points
2,425 Posts
07-01-2009 4:56 AM |
Hi HiPerFreak,
We reproduced your issue with your code. As a workaround, we can change your code like below.
DemoPresentationModel.cs
using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Collections.Generic; using System.ComponentModel; namespace ListBoxBugDemo { public class DemoPresentationModel : INotifyPropertyChanged { private List months; private Data selectedMonth; public List Months { get { return this.months; } } public Data SelectedMonth { get { return this.selectedMonth; } set { if (this.selectedMonth != value) { this.selectedMonth = value; OnPropertyChanged("SelectedMonth"); } } } public event PropertyChangedEventHandler PropertyChanged; public DemoPresentationModel(Page view) { this.months = new List() { new Data(1), new Data(2), new Data(3), new Data(4) }; this.selectedMonth = months[1]; view.Model = this; } protected virtual void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } public class Data { public Data(int i) { this.I = i; } public int I { get; set; } public override string ToString() { return this.I.ToString(); } } }
Page.xaml.cs
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; namespace ListBoxBugDemo { public partial class Page : UserControl { public Page() { InitializeComponent(); Loaded += new RoutedEventHandler(Page_Loaded); } void Page_Loaded(object sender, RoutedEventArgs e) { } public DemoPresentationModel Model { get { return DataContext as DemoPresentationModel; } set { DataContext = value; } } private void HandleShowSelectedClick(object sender, RoutedEventArgs e) { MessageBox.Show("listBox.SelectedItem = " + listBox.SelectedItem); } private void HandleSetTo11Click(object sender, RoutedEventArgs e) { DemoPresentationModel dpm = (DemoPresentationModel)this.DataContext; listBox.SelectedItem = dpm.Months[2]; } } }
Please add the styles and have a try. If it doesn't work, please share the demo just as you did on your initial post. Thanks
Best regards,
Jonathan
07-02-2009 1:01 AM |
Hi, thank you very much for your reply. Your work arround aoivds the problem of course (as I said in my first post, this bug only converns value types). I hoped there was some nicer work arround. Anyway, if I do it the way you said, it works. BUT if I style my app with the ImplicitStyleManager and apply the WhistlerBlue theme, it is broken with reference types too!
You can download my modified solution here (click on the "Right click here to ..." link at the top).
Any help would be much appreciated.
07-02-2009 4:48 AM |
Hi HiperFreak,
Ok. If you are using Silverlight 2, it won't work if we use ImplicitStyleManager just as you did in your last demo. The workaround is move new DemoPresentationModel(view); from App.xaml.cs to Page_Loaded event. For example,
void Page_Loaded(object sender, RoutedEventArgs e) { new DemoPresentationModel(this); }
By the way, this issue will be fixed with the coming up Silverlight 3 RTW. Actually, I have updated my Silverlight 3 beta environments to Silverlight 3 (3.0.40624.0 , for internal only) and your sample works pretty well with Silverlight Toolkit for Silverlight 3. So, just do some tiny modifications and waiting for the RTW version. By the way, I cannot assure you the release date. Thanks.