当项目添加到列表框中时将调用的事件处理程序 [英] Event handler that will be called when an item is added in a listbox

查看:123
本文介绍了当项目添加到列表框中时将调用的事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



谢谢!

解决方案

问题是包含事件处理程序的 INotifyCollectionChanged 接口是明确实现的,这意味着你必须先在事件处理程序可以使用之前转换ItemCollection:

  public MyWindow()
{
InitializeComponent );

((INotifyCollectionChanged)mListBox.Items).CollectionChanged + =
mListBox_CollectionChanged;
}

private void mListBox_CollectionChanged(object sender,
NotifyCollectionChangedEventArgs e)
{
if(e.Action == NotifyCollectionChangedAction.Add)
{
//将新项目滚动到视图
mListBox.ScrollIntoView(e.NewItems [0]);
}
}



乔希的's's's's's's's's's's's's还应考虑有关可观察收集的建议。


Is there an event handler that will be called when an item is added in a listbox in WPF?

Thanks!

解决方案

The problem is that the INotifyCollectionChanged interface which contains the event handler is explicitly implemented, which means you have to first cast the ItemCollection before the event handler can be used:

public MyWindow()   
{   
    InitializeComponent();   

    ((INotifyCollectionChanged)mListBox.Items).CollectionChanged +=   
        mListBox_CollectionChanged;   
}   

private void mListBox_CollectionChanged(object sender,    
    NotifyCollectionChangedEventArgs e)   
{   
    if (e.Action == NotifyCollectionChangedAction.Add)   
    {   
        // scroll the new item into view   
        mListBox.ScrollIntoView(e.NewItems[0]);   
    }       
}

Ref.

Josh's advice about the observable collection should also be considered.

这篇关于当项目添加到列表框中时将调用的事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆