Silverlight中绑定ComboBox.SelectedItem

5
这个让我疯狂。以下是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 ) );
        }
    }
}

页面加载时,ComboBox中选择了“Stuff 3”。单击按钮后,ComboBox中的项目会更改,但应该选择“Stuff 6”。然而,实际上未选中任何项目。


如果你需要安慰的话,这个在WPF中对我有效(我会看看Silverlight)。 - Ray
2个回答

14

试试这个:

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

SelectedItem不喜欢被绑定为OneWay。我还没有在Silverlight 2中尝试过,但是在Silverlight 3中,如果您不使用TwoWay绑定,甚至会出现黄色死亡三角形。


刚刚验证了一下,在SL2中解决了这个问题。发现得不错。 - Ray
这在这个例子中可以解决问题,但在我的大型项目中却不行。不确定那里出了什么问题。 - Josh Santangelo
如果您能提供更多关于您的大型项目与此处提供的示例不同之处的详细信息,那么我们可能能够更好地诊断问题。 - markti
我在stackoverflow上发布了一个关于此问题的新问题,其中包含更多信息:https://dev59.com/fkfRa4cB1Zd3GeqP-ZYF - Josh Santangelo

0

当然,绑定必须是双向的。但是将ItemsSource设置为控件并不意味着DataContext(SelectedItem变量应指向的位置)已经设置。确保组合框的DataContext已经正确设置。(在我的情况下,设置为Page。因为selectedStuff是一个Page属性。)

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接