<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);
}
}
<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);
}
}
解决方案 »
- C# hook recv
- DataTable dt = new DataTable("Resouce")这里的Resouce是什么意思 ?
- c#怎么通过button直接打开excel
- C#怎么调用这个API函数?
- 怎样让程序支持多种打印机?
- 添加不了“ASP.NET AJAX-Enabled Web Application”项目模版
- MessageBox中如果选中了Yes,如何得到返回值数字“6”呢?
- 高手帮忙,在XP下安装VS.NET(3遍都失败)的问题
- 如何开发一个继承datatable,可序列化的组件?
- 我装了WINXP Beta2后,我在这个操作环境想装MICROSOFT.NET,但更新不了部件!
- 大量画控件后出现红色的×,然后windows无法打开任何窗口
- webservice参数请教
如果你想下面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>