如何通过按钮替换列表视图中的滚动条可以滚动wpf c#中的项目

时间:2016-09-14 17:11:23

标签: c# wpf listview

你好我试着创建一个项目有一个列表视图和它的项目水平排序,我想要禁用列表的滚动条,并用2个按钮替换它们使项目向右,另一个使项目转到左that what i try to do
我在一篇文章中读到了这段代码,但它给出了一个错误

private void next_Click(object sender, RoutedEventArgs e)
    {
        if (ls.SelectedIndex == -1 || ls.SelectedIndex == ls.Items.Count - 1)
            return;

        Object select, next, temp;
        select = ls.Items[ls.SelectedIndex];
        next = ls.Items[ls.SelectedIndex + 1];

        temp = select;
        select = next;
        next = temp;

        ls.Items[ls.SelectedIndex] = select;
        ls.Items[ls.SelectedIndex + 1] = next;

        ls.SelectedIndex++;
    }

    private void back_Click(object sender, RoutedEventArgs e)
    {
        if (ls.SelectedIndex == -1 || ls.SelectedIndex == 0)
            return;

        Object select, previous, temp;
        select = ls.Items[ls.SelectedIndex];
        previous = ls.Items[ls.SelectedIndex - 1];

        temp = select;
        select = previous;
        previous = temp;

        ls.Items[ls.SelectedIndex] = select;
        ls.Items[ls.SelectedIndex - 1] = previous;

        ls.SelectedIndex--;
    }

所以有任何解决方案

请帮助:)

1 个答案:

答案 0 :(得分:0)

要更改ListView,请创建如下样式:

        <Style TargetType="ListView" x:Key="VerticalListView">
            <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
            <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Disabled" />

            <Setter Property="ScrollViewer.HorizontalScrollMode" Value="Enabled" />
            <Setter Property="ScrollViewer.VerticalScrollMode" Value="Disabled" />

            <Setter Property="ScrollViewer.ZoomMode" Value="Disabled" />
            <Setter Property="SelectionMode" Value="None" />

            <Setter Property="ItemsPanel">
                <Setter.Value>
                    <ItemsPanelTemplate>
                        <StackPanel VerticalAlignment="Top" Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                </Setter.Value>
            </Setter>

            <Setter Property="ItemContainerStyle">
                <Setter.Value>
                    <Style TargetType="ListViewItem">
                        <Setter Property="Height" Value="400" />
                    </Style>
                </Setter.Value>
            </Setter>
        </Style>

所以你可以像这样在xaml中应用样式:

<ListView Style="{StaticResource VerticalListView}" />

要通过代码选择itens,可以使用listView.SelectedIndex属性。 例如,继续前进:

if (myListView.SelectedIndex+1 > myListView.Items.Count)
{
   myListView.SelectedIndex=0;
}
else
{
  myListView.SelectedIndex++;
}

myListView.ScrollIntoView(myListView.Items[myListView.SelectedIndex]); 

我希望这对你有所帮助。