在使用自定义行为时应用ListViewItem的默认样式

时间:2013-04-07 01:42:52

标签: c# wpf listview attachedbehaviors

我正在使用自定义行为将列表视图项滚动到ListView的当前可见视图中:

mvvm how to make a list view auto scroll to a new Item in a list view

<UserControl.Resources>
    <Style x:Key="ListBoxItemContainerStyle" TargetType="{x:Type ListBoxItem}"
        BasedOn="{StaticResource {x:Type ListBoxItem}}">
        <Setter Property="localBehaviors:ListBoxItemBehavior.IsBroughtIntoViewWhenSelected" Value="true"/>
    </Style>
</UserControl.Resources>

<!-- using Caliburn.Micro's conventions to bind ItemsSource -->
<ListView x:Name="Items" ItemContainerStyle="{StaticResource ListBoxItemContainerStyle}"
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Column #1" DisplayMemberBinding="{Binding ValueOfColumnOne}" />
            <!-- the rest of columns are defined here using DisplayMemberBinding or GridViewColumn.CellTemplate -->
        </GridView
    </ListView.View>
</ListView>

问题在于,如本问题所述,列表视图项目会丢失其显示定义(我猜模板):

ListView losing GridView display when applying ItemContainerStyle

但是,我不想重新定义ListViewItem的模板;即我想使用WPF提供的默认值。

1 个答案:

答案 0 :(得分:0)

你不能只使用ScrollIntoView吗?然后关注它。

listview.ScrollIntoView(listviewitem);
listviewitem.Focus();