在Silverlight中绑定ComboBox.SelectedItem

时间:2009-05-13 00:29:31

标签: silverlight data-binding combobox selecteditem

这个让我发疯。这是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>

代码隐藏:

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 ) );
        }
    }
}

当页面加载时,会选择一个带有“Stuff 3”的ComboBox。单击该按钮时,ComboBox中的项目会更改,但应选择“Stuff 6”。相反,没有选择任何东西。

2 个答案:

答案 0 :(得分:13)

试试这个:

 <ComboBox ItemsSource="{Binding Path=Thing.Stuff}"                
      SelectedItem="{Binding Path=Thing.SelectedStuff, Mode=TwoWay}">

SelectedItem不喜欢被绑定OneWay。我没有机会在Silverlight 2中试用它,但在Silverlight 3中,如果不使用TwoWay绑定,你甚至会得到死亡的黄色三角形。

答案 1 :(得分:0)

当然绑定必须是TwoWay。但是将ItemsSource设置为控件并不意味着设置了DataContext(SelectedItem变量应指向的位置)。确保组合的DataContext设置良好。 (在我的例子中,是Page。由于selectedStuff是一个Page属性。

cmbStuff.DataContext = Me '(from  Page.Load)