Wpf ScrollIntoView第一次不工作

时间:2017-03-31 08:25:37

标签: c# wpf

我有一个ListView包含Expander,我分配了一个SelectedIndex,然后调用ScrollIntoView到SelectedItem位置。 (它可以自动扩展。)

每次都调用lv_SelectionChanged。

private void lv_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        foreach (Expander exp in FindVisualChildren<Expander>(lv.lv))
        {
            var a = (exp.Header as StackPanel).Children[0] as TextBlock;
            if (a.Text.Equals((lv.lv.SelectedItem as User).group))
                exp.IsExpanded = true;
        }
        lv.ScrollIntoView(lv.SelectedItem);
    }
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
        {
            if (depObj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                    if (child != null && child is T)
                    {
                        yield return (T)child;
                    }

                    foreach (T childOfChild in FindVisualChildren<T>(child))
                    {
                        yield return childOfChild;
                    }
                }
            }
        }

但是,它第一次没有工作。 (如果它是一个简单的ListView,它可以工作!)

我不知道。

THX。

XAML

<UserControl.Resources>
    <Style TargetType="{x:Type GridViewColumnHeader}">
        <Setter Property="HorizontalContentAlignment" Value="Center" />
    </Style>
    <Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <!--<Expander IsExpanded="{Binding Mode=TwoWay, Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}, Mode=FindAncestor}}" MouseRightButtonDown="Expander_MouseRightButtonDown">-->
                    <Expander IsExpanded="False"  MouseRightButtonDown="Expander_MouseRightButtonDown">
                        <Expander.Header>
                            <StackPanel Orientation="Horizontal">
                                <StackPanel.ContextMenu>
                                    <ContextMenu>
                                        <MenuItem Name="mi_ExpandAll" Header="Hide Age column" Click="mi_ExpandAll_Click"/>
                                        <MenuItem Name="mi_CollapseAll" Header="None"/>
                                    </ContextMenu>
                                </StackPanel.ContextMenu>
                                <TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="Gray" FontSize="16" VerticalAlignment="Bottom"/>
                                <TextBlock Text="{Binding ItemCount}" FontSize="22" Foreground="Green" FontWeight="Bold" FontStyle="Italic" Margin="10,0,0,0" VerticalAlignment="Bottom" />
                                <TextBlock Text=" item(s)" FontSize="22" Foreground="Silver" FontStyle="Italic" VerticalAlignment="Bottom" />
                            </StackPanel>
                        </Expander.Header>
                        <ItemsPresenter>
                            <ItemsPresenter.ContextMenu>
                                <ContextMenu>
                                    <MenuItem Header="Add Item" Click="MenuItem_Click"/>
                                    <MenuItem Header="Menu Item 2">
                                        <MenuItem Header="Remove Item" Click="MenuItem_Click_1"></MenuItem>
                                        <MenuItem Header="Select last Item" Click="MenuItem_Click_2"></MenuItem>
                                    </MenuItem>
                                </ContextMenu>
                            </ItemsPresenter.ContextMenu>
                        </ItemsPresenter>
                    </Expander>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>
<ListView Margin="10" Name="lv" Grid.Row="0" ItemsSource="{Binding GroupView}" SelectedIndex="{Binding Index}" SelectedItem="{Binding Item}" SelectionChanged="lv_SelectionChanged">
            <ListView.View>
                <GridView>
                    <local:GridViewColumnExt Header="Name" Width="120" DisplayMemberBinding="{Binding Name}"/>
                    <local:GridViewColumnExt x:Name="colAge" Header="Age" Width="50">
                        <local:GridViewColumnExt.CellTemplate>
                            <DataTemplate>
                                <Button Content="{Binding Age}"></Button>
                            </DataTemplate>
                        </local:GridViewColumnExt.CellTemplate>
                    </local:GridViewColumnExt>
                </GridView>
            </ListView.View>
            <ListView.GroupStyle>
                <GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
                </GroupStyle>
            </ListView.GroupStyle>
        </ListView>

我在事件

中设置了SelectedIndex
private void MenuItem_Click_2(object sender, RoutedEventArgs e)
    {
        lv.SelectedIndex = 8;
    }

修改

我尝试设置IsExpanded =&#34; True&#34;然后就可以了。所以,如果IsExpanded =&#34; False&#34;首先,它可以滚动吗?

1 个答案:

答案 0 :(得分:0)

我用户expandder.BringIntoView(),然后就可以了!