我最近用DataGrid做了這個。訣竅是將自定義行爲附加到您的視圖。像那樣。
<ListView ... >
<iy:Interaction.Behaviors>
<ext:ScrollIntoViewBehavior />
</iy:Interaction.Behaviors>
...
</ListView>
而且assocciated類:
public class ScrollIntoViewBehavior : Behavior<ListView>
{
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.SelectionChanged += new SelectionChangedEventHandler(AssociatedObject_SelectionChanged);
}
protected override void OnDetaching()
{
base.OnDetaching();
this.AssociatedObject.SelectionChanged -= new SelectionChangedEventHandler(AssociatedObject_SelectionChanged);
}
private void AssociatedObject_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (sender is ListView)
{
ListView grid = (sender as ListView);
if (grid.SelectedItem != null)
{
grid.Dispatcher.BeginInvoke(() =>
{
grid.UpdateLayout();
grid.ScrollIntoView(grid.SelectedItem);
});
}
}
}
}
有些事情需要注意的:
該文檔說的是什麼? – 2013-05-08 07:24:16
你能提供一個代碼示例嗎?您是否將ViewModel屬性(例如MySelectedItem)綁定到ListView SelectedItem屬性? – mamuesstack 2013-05-08 08:00:11