在Silverlight中绑定ComboBox.SelectedItem

在Silverlight中绑定ComboBox.SelectedItem,第1张

概述这个让我发疯.这是XAML: <UserControl x:Class="SilverlightApplication1.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 这个让我发疯.这是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”.相反,没有选择任何东西.

解决方法 试试这个:

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

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

总结

以上是内存溢出为你收集整理的在Silverlight中绑定ComboBox.SelectedItem全部内容,希望文章能够帮你解决在Silverlight中绑定ComboBox.SelectedItem所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1000958.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存