Dan*_*nte 16 c# wpf events listview
我正在使用WPF,我正在使用ListView,我需要在项目添加到它时触发事件.我试过这个:
var dependencyPropertyDescriptor = DependencyPropertyDescriptor.FromProperty(ItemsControl.ItemsSourceProperty, typeof(ListView));
if (dependencyPropertyDescriptor != null)
{
dependencyPropertyDescriptor.AddValueChanged(this, ItemsSourcePropertyChangedCallback);
}
Run Code Online (Sandbox Code Playgroud)
.....
private void ItemsSourcePropertyChangedCallback(object sender, EventArgs e)
{
RaiseItemsSourcePropertyChangedEvent();
}
Run Code Online (Sandbox Code Playgroud)
但它似乎只在整个集合发生变化时起作用,我已经阅读过这篇文章:event-fired-when-item-is-added-to-listview,但最佳答案仅适用于listBox.我试图将代码更改为ListView但我无法做到这一点.
我希望你能帮助我.先感谢您.
Dan*_*nte 53
请注意,这仅适用于WPF Listview!
经过一些研究,我找到了我的问题的答案,这很简单:
public MyControl()
{
InitializeComponent();
((INotifyCollectionChanged)listView.Items).CollectionChanged += ListView_CollectionChanged;
}
private void ListView_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
// scroll the new item into view
listView.ScrollIntoView(e.NewItems[0]);
}
}
Run Code Online (Sandbox Code Playgroud)
实际上,NotifyCollectionChangedAction枚举允许您的程序通知您任何更改,例如:添加,移动,替换,删除和重置.
| 归档时间: |
|
| 查看次数: |
21335 次 |
| 最近记录: |