<Window.Resources >
        <cc:ClassInfo   x:Key="ci">
            <cc:ClassInfo.Name>Tom</cc:ClassInfo.Name>
            <cc:ClassInfo.ColorName>Red</cc:ClassInfo.ColorName>
        </cc:ClassInfo>
        <cc:ClassInfoCollection x:Name="MyCollection" x:Key="coll"  >
            <cc:ClassInfo ColorName="ooo22yyy" Name="ccccc3333YYY"></cc:ClassInfo>
        </cc:ClassInfoCollection>
 </Window.Resources >
<StackPanel>
<ListBox Name="lsbMe" ItemsSource="{StaticResource coll}"
         Height="300" IsSynchronizedWithCurrentItem="True">
            <ListBox.ItemTemplate>
                <DataTemplate>
                <Grid>
                <WrapPanel> //下面的数据在修改后,源数据没有更新,其绑定的对象不是同一个对象
                    <Label>Name:</Label>
                    <TextBox  Width="120"  Text="{ Binding Path=Name}">
                    </TextBox>
                    <Label>ColorName:</Label>
                    <TextBox  Width="120"  Text="{Binding Path=ColorName }"></TextBox>
                </WrapPanel>
            </Grid>
        </DataTemplate>
                </ListBox.ItemTemplate>
        </ListBox>
                <WrapPanel>
            <TextBox Name="txtName" Width="120" 
                     Text="{Binding Source={StaticResource ci},Path=Name}"></TextBox>
            <TextBox Name="txtColorName" Width="120" 
                     Text="{Binding Source={StaticResource ci},Path=ColorName}" ></TextBox>
            <Button Name="btnShow" Click="btnShow_Click">显示源数据</Button>
        </WrapPanel>
    </StackPanel>//绑定源类型
public class ClassInfo : INotifyPropertyChanged
    {
        public ClassInfo()
        {
            _PropertyChanged += new PropertyChangedEventHandler(ClassInfo__PropertyChanged);
        }        void ClassInfo__PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            object obj = sender;
        }        string _Name = "";        public string Name
        {
            get { return _Name; }
            set 
            { 
                _Name = value; 
            }
        }        string _ColorName = "";        public string ColorName
        {
            get { return _ColorName; }
            set { _ColorName = value; }
        }
        DataRowState _State = DataRowState.Unchanged;
        public DataRowState State
        {
            get { return _State; }
            set { _State = value; }
        }        public override string ToString()
        {
            return this.Name;
        }
        #region INotifyPropertyChanged 成员
        event PropertyChangedEventHandler _PropertyChanged = null;
        event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged
        {
            add { _PropertyChanged += value; }
            remove { _PropertyChanged -= value; }
        }        #endregion
    }    public class ClassInfoCollection :  BindingList<ClassInfo>
    {
        public ClassInfoCollection()
        {
            ClassInfo ci = null;
            
            ci = new ClassInfo();
            ci.Name = "aaaaaaaa";
            ci.ColorName = "Red";
            ci.State = DataRowState.Added;
            Add(ci);            ci = new ClassInfo();
            ci.Name = "bbbbbbbb";
            ci.ColorName = "Blue";
            ci.State = DataRowState.Deleted;
            Add(ci);
        }
    }

解决方案 »

  1.   

    数据源是一个IList , Binding后,进行Add()操作,UI也能看到, 但在UI上对IList中的某一对象进行编辑后,查询看IList中的对象数据却没有得到更新,比较了Bindin到TextBox的对象和IList中的对象,却不是同一个引用,望请各位解答,先谢了
      

  2.   

    其实我没看懂你的意思,我只是顺着你意思大概整理了一下代码。
    如果你想下面WrapPanel里的TextBox里的内容和上面ListBox同步的话,那么它们的数据源必须严格一致。public class ClassInfo : INotifyPropertyChanged
    {    public string Name { get; set; }
        public string ColorName { get; set; }
        #region INotifyPropertyChanged 成员    public event PropertyChangedEventHandler PropertyChanged;    
        public void OnPropertyChanged(string propertyName)
        { 
            if(PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName);
        
        }    #endregion    public override string ToString()
        {
            return this.Name;
        }
    }public class ClassInfoCollection : System.Collections.ObjectModel.ObservableCollection<ClassInfo>
    {
        public ClassInfoCollection()
        {
            Add( new ClassInfo { Name = "aaaaaaaa",  ColorName = "Red" } );
            Add(new ClassInfo { Name = "bbbbbbbb", ColorName = "Blue" });        
        }
    }<Window.Resources >
        <!--<cc:ClassInfo x:Key="ci" Name="Tom" ColorName="Red">
        </cc:ClassInfo>-->
        <cc:ClassInfoCollection x:Name="MyCollection" x:Key="coll" >
            <cc:ClassInfo ColorName="ooo22yyy" Name="ccccc3333YYY"></cc:ClassInfo>
        </cc:ClassInfoCollection>
    </Window.Resources>
    <StackPanel DataContext="{StaticResource coll}">
        <ListBox Name="lsbMe" ItemsSource="{Binding}" Height="300" IsSynchronizedWithCurrentItem="True">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <WrapPanel>
                            <Label>Name:</Label>
                            <TextBox Width="120" Text="{ Binding Path=Name}">
                            </TextBox>
                            <Label>ColorName:</Label>
                            <TextBox Width="120" Text="{Binding Path=ColorName }"></TextBox>
                        </WrapPanel>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>    <WrapPanel>
            <TextBox Name="txtName" Width="120" Text="{Binding Name}"></TextBox>
            <TextBox Name="txtColorName" Width="120" Text="{Binding ColorName}" ></TextBox>
            <Button Name="btnShow" Click="btnShow_Click">显示源数据</Button>
        </WrapPanel></StackPanel>