在Silverlight中绑定ComboBox.SelectedItem [英] Binding ComboBox.SelectedItem in Silverlight

查看:145
本文介绍了在Silverlight中绑定ComboBox.SelectedItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这一个让我疯狂。这里是XAML:

This one is driving me crazy. Here's the 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。当单击按钮时,组合框中的项目改变,但应选择填充6。

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.

推荐答案

尝试:

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

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

SelectedItem does not like to be bound OneWay. I haven't had a chance to try it out in Silverlight 2 but in Silverlight 3 you will even get the yellow triangle of death if you don't use TwoWay binding.

这篇关于在Silverlight中绑定ComboBox.SelectedItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆