La Liaison De Zone De Liste Déroulante.SelectedItem en Silverlight
C'est me rend fou. Voici le code XAML:
<UserControl x:Class="SilverlightApplication1.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid x:Name="LayoutRoot" Background="White">
<StackPanel HorizontalAlignment="Left" VerticalAlignment="Top">
<ComboBox ItemsSource="{Binding Path=Thing.Stuff}"
SelectedItem="{Binding Path=Thing.SelectedStuff}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Button Content="Again" Click="Button_Click" />
</StackPanel>
</Grid>
</UserControl>
Et le code-behind:
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<Stuff>();
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<Stuff> _Stuff;
public ObservableCollection<Stuff> 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 ) );
}
}
}
Lorsque le chargement de la page, il y a une zone de liste déroulante avec des "Choses " 3" est sélectionné. Lorsque le bouton est cliqué, les éléments dans la zone de liste déroulante changer, mais les "Choses " 6" doit être sélectionné. Au lieu de cela, rien n'est sélectionné.
- Si c'est une consolation, ça fonctionne pour moi en WPF (je vais voir sur Silverlight).
Vous devez vous connecter pour publier un commentaire.
Essayez ceci:
SelectedItem n'aime pas être lié OneWay. Je n'ai pas eu la chance de l'essayer dans Silverlight 2, mais dans Silverlight 3, vous pourrez même obtenir le triangle jaune de la mort si vous n'utilisez pas de liaison Bidirectionnelle.
Bien sûr, la liaison doit être Bidirectionnelle. Mais la définition d'un ItemsSource à un contrôle ne signifie pas que le DataContext (où la variable SelectedItem doivent pointer vers l') est définie. S'assurer que le combo du DataContext est bien définie. (dans mon cas, à la Page. Comme selectedStuff est une propriété de Page.