我在这个链接上找到了
ObservableCollection没有注意到它中的Item何时发生变化(即使使用INotifyPropertyChanged)
一些通知Observablecollection项目已更改的技术.这个链接中的TrulyObservableCollection似乎正是我正在寻找的.
public class TrulyObservableCollection<T> : ObservableCollection<T>
where T : INotifyPropertyChanged
{
public TrulyObservableCollection()
: base()
{
CollectionChanged += new NotifyCollectionChangedEventHandler(TrulyObservableCollection_CollectionChanged);
}
void TrulyObservableCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
foreach (Object item in e.NewItems)
{
(item as INotifyPropertyChanged).PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
}
}
if (e.OldItems != null)
{
foreach (Object item in e.OldItems)
{
(item as INotifyPropertyChanged).PropertyChanged -= new PropertyChangedEventHandler(item_PropertyChanged);
}
}
}
void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
NotifyCollectionChangedEventArgs a = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
OnCollectionChanged(a); …Run Code Online (Sandbox Code Playgroud) c# collections wpf observablecollection inotifypropertychanged
最好的解释方法是举例如下:
这是模型
public class Person
{
public int age;
public string name;
}
Run Code Online (Sandbox Code Playgroud)
这是视图模型
public class PersonVM
{
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:
vm应该将人员暴露给数据模板还是用他自己的属性封装模型属性?
我花了很多时间尝试为以下挑战找到一个优雅的解决方案.我一直无法找到解决问题的解决方案.
我有一个简单的View,ViewModel和Model设置.为了便于解释,我会保持简单.
Model有一个名为TitleString类型的属性.Model是在DataContext的View.View有TextBlock这就是数据绑定到Title的模型.ViewModel有一个名为方法Save(),将保存Model到ServerServer可推到所做的更改Model到现在为止还挺好.现在我需要进行两项调整才能使模型与a保持同步Server.服务器的类型并不重要.只要知道我需要调用Save()才能将模型推送到Server.
调整1:
Model.Title属性将需要调用RaisePropertyChanged(),以转换为所做的更改Model由Server到View.这很好用,因为它Model是DataContextView还不错.
调整2:
Save()保存从所做的更改View对Model上Server.这是我被卡住的地方.当模型被更改时,我可以处理调用Save()的Model.PropertyChanged事件,ViewModel但这会使它回显服务器所做的更改.我正在寻找一个优雅而合理的解决方案,如果有意义,我愿意改变我的架构.
我习惯用N层架构开发,即数据访问层,业务逻辑层等
任何人都可以提供有关我的业务逻辑的最佳位置的任何建议或链接吗?
我是否将所有这些都放入Silverlight应用程序的Models文件夹中的类中?
保罗
我试图在ListView中显示一些图像,但没有成功.我正在使用WPF MVVM,而ListView是简单地显示套装和排名数据的延续.(请参阅我之前的帖子:WPF中的MVVM - 如何提醒ViewModel模型中的更改......或者我应该?如果您感兴趣的话!)也就是说,我可以使用ListView以外的东西(如果这是建议)但我仍然想知道如何使用ListView,假设它是可行的.我在ViewModel中绑定的属性是:
public ObservableCollection<Image> PlayerCardImages{
get{
ObservableCollection<Image> results = new ObservableCollection<Image>();
foreach (CardModel card in PlayerCards)
{
Image img = new Image();
BitmapImage bi3 = new BitmapImage();
bi3.BeginInit();
// TODO: Pick card based on suit/rank. Just get 1 image working now
bi3.UriSource = new Uri("diamond-1.png", UriKind.Relative);
bi3.EndInit();
img.Stretch = Stretch.Fill;
img.Source = bi3;
results.Add(img);
}
return results;
}
}
Run Code Online (Sandbox Code Playgroud)
在我使用的XAML代码中:
<Window.Resources>
<DataTemplate x:Key="ImageCell">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding PlayerCardImages}" Width="200" Height="200" Stretch="Fill" ToolTip="Add tooltip"/>
</StackPanel> …Run Code Online (Sandbox Code Playgroud)