Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding ComboBox.SelectedItem in Silverlight
    primarykey
    data
    text
    <p>This one is driving me crazy. Here's the XAML:</p> <pre><code> &lt;UserControl x:Class="SilverlightApplication1.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt; &lt;Grid x:Name="LayoutRoot" Background="White"&gt; &lt;StackPanel HorizontalAlignment="Left" VerticalAlignment="Top"&gt; &lt;ComboBox ItemsSource="{Binding Path=Thing.Stuff}" SelectedItem="{Binding Path=Thing.SelectedStuff}"&gt; &lt;ComboBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock Text="{Binding Path=Name}" /&gt; &lt;/DataTemplate&gt; &lt;/ComboBox.ItemTemplate&gt; &lt;/ComboBox&gt; &lt;Button Content="Again" Click="Button_Click" /&gt; &lt;/StackPanel&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>And the codebehind:</p> <pre><code>using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Windows; using System.Windows.Controls; namespace SilverlightApplication1 { public partial class Page : UserControl { public Page() { InitializeComponent(); Data data = new Data(); data.Thing = new Thing(); data.Thing.Stuff = new ObservableCollection&lt;Stuff&gt;(); data.Thing.Stuff.Add( new Stuff { Name = "Stuff 1" } ); data.Thing.Stuff.Add( new Stuff { Name = "Stuff 2" } ); data.Thing.Stuff.Add( new Stuff { Name = "Stuff 3" } ); data.Thing.SelectedStuff = data.Thing.Stuff.Last(); DataContext = data; } private void Button_Click( object sender, RoutedEventArgs e ) { Data data = ( DataContext as Data ); data.Thing.Stuff.Clear(); data.Thing.Stuff.Add( new Stuff { Name = "Stuff 4" } ); data.Thing.Stuff.Add( new Stuff { Name = "Stuff 5" } ); data.Thing.Stuff.Add( new Stuff { Name = "Stuff 6" } ); data.Thing.SelectedStuff = data.Thing.Stuff.Last(); } } public class Data : INotifyPropertyChanged { private Thing _Thing; public Thing Thing { get { return _Thing; } set { _Thing = value; NotifyPropertyChanged( "Thing" ); } } public event PropertyChangedEventHandler PropertyChanged; protected void NotifyPropertyChanged( string propertyName ) { if ( PropertyChanged == null ) { return; } PropertyChanged( this, new PropertyChangedEventArgs( propertyName ) ); } } public class Thing : INotifyPropertyChanged { private ObservableCollection&lt;Stuff&gt; _Stuff; public ObservableCollection&lt;Stuff&gt; Stuff { get { return _Stuff; } set { _Stuff = value; NotifyPropertyChanged( "Stuff" ); } } private Stuff _SelectedStuff; public Stuff SelectedStuff { get { return _SelectedStuff; } set { _SelectedStuff = value; NotifyPropertyChanged( "SelectedStuff" ); } } public event PropertyChangedEventHandler PropertyChanged; protected void NotifyPropertyChanged( string propertyName ) { if ( PropertyChanged == null ) { return; } PropertyChanged( this, new PropertyChangedEventArgs( propertyName ) ); } } public class Stuff : INotifyPropertyChanged { private string _Name; public string Name { get { return _Name; } set { _Name = value; NotifyPropertyChanged( "Name" ); } } public event PropertyChangedEventHandler PropertyChanged; protected void NotifyPropertyChanged( string propertyName ) { if ( PropertyChanged == null ) { return; } PropertyChanged( this, new PropertyChangedEventArgs( propertyName ) ); } } } </code></pre> <p>When the page loads, there is a ComboBox with "Stuff 3" selected. When the button is clicked, the items in the ComboBox change, but "Stuff 6" should be selected. Instead, nothing is selected.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload