所以有剝皮這隻貓至少有兩種方式:
- 也由克萊門斯在評論解釋了我的問題
- 請在this post by WPF Mentor
解決方案1看起來更自然對於事件訂閱,因爲您不需要投射;另外IntelliSense不會在沒有強制轉換的情況下顯示已實現接口的類成員,因此對於解決方案2,您必須記住要查看哪些接口已實現並檢查事件。這裏是預訂的樣子每個解決方案:
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
// Solution 1, subscription:
xActionListView.ItemContainerGenerator.ItemsChanged +=
new ItemsChangedEventHandler(ActionLog_ItemsChanged);
// Solution 2, subscription:
((INotifyCollectionChanged)xActionListView.Items).CollectionChanged +=
new NotifyCollectionChangedEventHandler(ActionListView_CollectionChanged);
}
但解決方案2具有更易於使用事件中的arg處理程序:
// Solution 1, handler:
private void ActionLog_ItemsChanged(object sender, ItemsChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
// Solution 1, scroll the new item into view
xActionListView.ScrollIntoView(
xActionListView.Items[e.Position.Index + e.Position.Offset]);
}
}
// Solution 2, handler:
private void ActionListView_CollectionChanged(
object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
// Solution 2, scroll the new item into view
xActionListView.ScrollIntoView(e.NewItems[0]);
}
}
它看起來像在某些情況下,一種解決方案可能更合適比另一個更重要:根據您需要的數據,事件數據可能更容易在一個或另一箇中使用。
如何處理ListView的[ItemContainerGenerator](http:/ /)的[ItemsChanged](http://msdn.microsoft.com/en-us/library/system.windows.controls.itemcontainergenerator.itemschanged.aspx) /msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemcontainergenerator.aspx)?也許還太早... – Clemens 2013-02-15 19:16:51
這似乎工作。但是,事件arg有點奇怪:e.Position是一個位置生成器,具有Index和Offset屬性,如果您對添加的最新元素感興趣,您似乎必須添加索引+偏移量。以下更多細節。 – Schollii 2013-02-16 13:53:27