none
数据绑定问题 RRS feed

  • 问题

  • 是这样一个问题.
    我在给ComboBox绑定ItemsSource时,绑定了一个List集合.
    在后面的工作中要操作这个集合来改变ComboBox中的显示.
    比如,在List中添加一条信息后,ComboBox中显示该信息..
    可问题就来了.
    如果我没把ComboBox下拉打开之前,向List中添加的信息在打开以后可以正确显示.
    当我打开以后,再向List中添加信息时,ComboBox中的显示便不改变了..

    当然,已经找到了解决方案.就是把ItemsSource = null;然后再换回来..
    可我不明白什么造成了这样的结果呢?
    是不是ComboBox第一次打开下拉时调用了某个生成Items的方法,以后再打开下拉时便不调用了..

    上代码,上真相.
    从我工程中的一段代码copy出来了.

    Window1.xaml:
    <Window x:Class="WpfApplication2.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="37*" />
                <RowDefinition Height="225*" />
            </Grid.RowDefinitions>
            <ComboBox Text="请选择可能的险情" x:Name="PossibleEndangers" IsEditable="True" Height="29" VerticalAlignment="Top">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <CheckBox Content="{Binding Path=Content}" IsChecked="{Binding Path=IsChecked}" Width="200" />
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
           
            <StackPanel Grid.Row="1">
                <TextBox x:Name="txt" />
                <CheckBox x:Name="ckb" />
                <Button Click="Button_Click" Content="添加" />
                <Button Click="Button_Click_1" Content="查看数量" />
                <Button Click="Button_Click_2" Content="刷新ComboBox" />
            </StackPanel>
        </Grid>
    </Window>
    Window1.xaml.cs:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Collections;

    namespace WpfApplication2
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
            }

            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                this.PossibleEndangers.ItemsSource = DataSource._Instance;
            }

            private void Button_Click(object sender, RoutedEventArgs e)
            {
                DataSource._Instance.Add(new Entity { Content = this.txt.Text.Trim(), IsChecked = (bool)this.ckb.IsChecked });
            }

            private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                MessageBox.Show((this.PossibleEndangers.ItemsSource as DataSource).Count().ToString());
            }

            private void Button_Click_2(object sender, RoutedEventArgs e)
            {
                this.PossibleEndangers.ItemsSource = null;
                this.PossibleEndangers.ItemsSource = DataSource._Instance;
            }
        }

        class Entity
        {
            private String _Content = String.Empty;

            public String Content
            {
                get { return _Content; }
                set { _Content = value; }
            }
            private bool _IsChecked = false;

            public bool IsChecked
            {
                get { return _IsChecked; }
                set { _IsChecked = value; }
            }
        }

        class DataSource : IEnumerable<Entity>
        {
            public static DataSource _Instance = new DataSource();

            private DataSource() { }

            private List<Entity> _Entities = new List<Entity>();

            public void Add(Entity entity)
            {
                this._Entities.Add(entity);
            }

            #region IEnumerable<Entity> Members

            IEnumerator<Entity> IEnumerable<Entity>.GetEnumerator()
            {
                return this._Entities.GetEnumerator();
            }

            #endregion

            #region IEnumerable Members

            IEnumerator IEnumerable.GetEnumerator()
            {
                return this._Entities.GetEnumerator();
            }

            #endregion
        }
    }


    有人说,充满技术的生活枯燥无味.. 我笑他们不懂.因为只有技术才能充实生活.. 学习就像生活,都需要善于总结,才能发现问题,取得进步.. 生活中充满了数学算式与结构,只要我们善于观察和思考..
    2009年12月14日 3:06

全部回复

  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using System.Collections;
    namespace WpfApplication1
    {
        /// <summary>
        /// Window17.xaml 的交互逻辑
        /// </summary>
        public partial class Window17 : Window
        {
            public Window17()
            {
                InitializeComponent();
            this.PossibleEndangers.ItemsSource =new  DataSource().lists();
            
            }
       private void Button_Click(object sender, RoutedEventArgs e)
            {
                (PossibleEndangers.ItemsSource as System.Collections.ObjectModel.ObservableCollection<Entity>).Add(new Entity { Content = this.txt.Text.Trim(), IsChecked = (bool)this.ckb.IsChecked });
               
       
       }
            private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                MessageBox.Show((this.PossibleEndangers.ItemsSource as ICollection<Entity>).Count().ToString());
            }
            private void Button_Click_2(object sender, RoutedEventArgs e)
            {
                this.PossibleEndangers.ItemsSource = null;
                this.PossibleEndangers.ItemsSource = DataSource._Instance;
            }
        }
       public  class Entity
        {
            private String _Content = String.Empty;
            public String Content
            {
                get { return _Content; }
                set { _Content = value; }
            }
            private bool _IsChecked = false;
            public bool IsChecked
            {
                get { return _IsChecked; }
                set { _IsChecked = value; }
            }
        }
        class DataSource : IEnumerable<Entity>
        {
            public static DataSource _Instance = new DataSource();
          public  DataSource() { }
            private System.Collections.ObjectModel.ObservableCollection <Entity> _Entities = new  System.Collections.ObjectModel.ObservableCollection<Entity>();
            public void Add(Entity entity)
            {
                this._Entities.Add(entity);
            }
            public ICollection<Entity> lists()
        {
                return _Entities;
        }
            #region IEnumerable<Entity> Members
            IEnumerator<Entity> IEnumerable<Entity>.GetEnumerator()
            {
                return this._Entities.GetEnumerator();
            }
            #endregion
            #region IEnumerable Members
            IEnumerator IEnumerable.GetEnumerator()
            {
                return this._Entities.GetEnumerator();
            }
            #endregion
        }
    
    }

     

    2009年12月14日 16:13
  • 要用ObservableCollection才可以的,
    要不就在你集合变化时触发INotifyCollectionChanged 接口公开 CollectionChanged 事件
    2010年1月6日 1:06