自定义ListView控件

发布于 2024-10-18 06:28:55 字数 206 浏览 3 评论 0原文

所以,我一直在寻找解决方案超过 12 个小时(但没有成功)。我应该如何更改 ListView ControlTemplate 以获得如下效果:

在此处输入图像描述

(这个问题是关于这个按钮的工作原理滚动视图)

您还有其他想法如何创建这样的控件吗?

So, I have been looking for solution more than 12 hours(but without success). How should I change ListView ControlTemplate to get effect like this:

enter image description here

(This question is about this buttons that working like scrollview)

Have you another ideas how to create control like this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

燃情 2024-10-25 06:28:55

它是垂直表示,但想法是可以理解的:隐藏滚动条并手动操作它们。为了获得更灵敏的 UI,您需要订阅 MouseDown 事件而不是 Click,而且 Grid_Click() 的每一行都可能出现 NullReference 异常。

XAML:

        <ListView.Template>
            <ControlTemplate>
                <Grid ButtonBase.Click="Grid_Click">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="16"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="16"/>
                    </Grid.RowDefinitions>
                    <Button Content="^" Grid.Row="0"/>
                    <Button Content="v" Grid.Row="2"/>
                    <ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Hidden">
                        <ItemsPresenter/>
                    </ScrollViewer>
                </Grid>
            </ControlTemplate>
        </ListView.Template>

代码:

    private void Grid_Click(object sender, RoutedEventArgs e) {
        bool down = (e.OriginalSource as Button).Content as string == "v";
        var scroller = VisualTreeHelper.GetChild((e.OriginalSource as Button).Parent, 2) as ScrollViewer;
        scroller.ScrollToVerticalOffset(scroller.VerticalOffset + (down ? 1 : -1));
    }

GetChild() 中的神奇数字 2 是 ScrollViewer 在其父项(Grid)内的索引。

It's vertical representation, but idea is understood: hide scrollbars and manipulate them manually. For more responsive UI you'll need to subscribe to MouseDown event instead of Click, also NullReference exceptions are possible on every line of Grid_Click().

XAML:

        <ListView.Template>
            <ControlTemplate>
                <Grid ButtonBase.Click="Grid_Click">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="16"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="16"/>
                    </Grid.RowDefinitions>
                    <Button Content="^" Grid.Row="0"/>
                    <Button Content="v" Grid.Row="2"/>
                    <ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Hidden">
                        <ItemsPresenter/>
                    </ScrollViewer>
                </Grid>
            </ControlTemplate>
        </ListView.Template>

Code:

    private void Grid_Click(object sender, RoutedEventArgs e) {
        bool down = (e.OriginalSource as Button).Content as string == "v";
        var scroller = VisualTreeHelper.GetChild((e.OriginalSource as Button).Parent, 2) as ScrollViewer;
        scroller.ScrollToVerticalOffset(scroller.VerticalOffset + (down ? 1 : -1));
    }

Magical number 2 in GetChild() is index of ScrollViewer inside its parent (Grid).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文