WPF ListBox هل يمكنك جعلها دورة؟ أي عدم ضرب توقف شديد في أعلى وأسفل

StackOverflow https://stackoverflow.com/questions/389341

سؤال

لدي ListBox WPF مرتبط بكائن البيانات. داخل القائمة هي سلسلة من الصور مع النص. يتم وضعه في أزياء أفقية، ورفض الجانبين على الجانبين الأيسر أو الأيمن من المربع، قم بتمرير العناصر المتبقية أو اليمين على التوالي.

دعنا نقول أن هناك 20 مادة في قائمة القائمة. أحاول معرفة كيفية الضغط على الموضع 19 عنصر (0 مقرها) يمكنني إجراء دورة في المربع وبدء تشغيل المجموعة، بحيث يذهب 1 -19 وما إلى ذلك. سيحتاج أيضا إلى الدورة في الاتجاه الآخر، بحيث كنت في البند 0، وتمرق اليسار، ستحصل على 19.

لقد جربت keyboardnavigation.directionalnavigation = "دورة" ولكن هذا لا يبدو أن تفعل أي شيء بالنسبة لي، وكنت أتجه في القش لأن هذا لا علاقة له بلوحة المفاتيح، كل شيء على أساس الماوس.

        <ListBox ItemsSource="{Binding Source={StaticResource WPFApparelCollection}}" Margin="24,-7,39,-19" ScrollViewer.VerticalScrollBarVisibility="Hidden" ScrollViewer.HorizontalScrollBarVisibility="Hidden" SelectionMode="Single" x:Name="list1" MouseLeave="List1_MouseLeave" MouseMove="List1_MouseMove" Style="{DynamicResource ListBoxStyle1}" Background="Transparent" BorderThickness="0">
            <ListBox.Resources>
                <!-- override the system brushes so that selected items are transparent whether the ListBox has focus or not -->
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
            </ListBox.Resources>
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="Background" Value="Transparent" />
                    <Setter Property="HorizontalContentAlignment" Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
                    <Setter Property="VerticalContentAlignment" Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
                    <Setter Property="Padding" Value="20,10,20,10" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                <Border x:Name="Bd" SnapsToDevicePixels="true" Background="Transparent" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                                </Border>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsSelected" Value="true">
                                        <Setter Property="Background" TargetName="Bd" Value="Transparent" />
                                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
                                    </Trigger>
                                    <MultiTrigger>
                                        <MultiTrigger.Conditions>
                                            <Condition Property="IsSelected" Value="true" />
                                            <Condition Property="Selector.IsSelectionActive" Value="false" />
                                        </MultiTrigger.Conditions>
                                        <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
                                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
                                    </MultiTrigger>
                                    <Trigger Property="IsEnabled" Value="false">
                                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel Orientation="Horizontal" IsItemsHost="True" />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical">
                        <Image Source="{Binding Image}" MouseLeave="Image_MouseLeave" MouseEnter="Image_MouseEnter" Cursor="Hand" Tag="{Binding Link}" MouseLeftButtonDown="Image_MouseLeftButtonDown" VerticalAlignment="Top" HorizontalAlignment="Left"></Image>
                        <Label Content="{Binding Name}" Cursor="Hand" Tag="{Binding Link}" MouseLeftButtonDown="Label_MouseLeftButtonDown" VerticalAlignment="Bottom" Foreground="White" Style="{StaticResource Gotham-Medium}" FontSize="8pt" HorizontalAlignment="Center" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
هل كانت مفيدة؟

المحلول

القضية هنا ليست حقا مع listBox., ، ولكن مع scrollview. داخل قالب التحكم؛ لذلك لجعل دورة العناصر التي ستحتاج إلى تغيير scrollview. بطريقة ما. لقد كتبت عنصر تحكم مستمد من scrollview. هذه الدورات في اتجاه عمودي ... ولكن ينبغي أن يكون من السهل معرفة كيفية جعلها تعمل أفقيا أيضا.

public class CyclicScrollViewer : ScrollViewer
{
    public CyclicScrollViewer()
    {
        this.CommandBindings.Add(new CommandBinding(ScrollBar.LineUpCommand, LineCommandExecuted));
        this.CommandBindings.Add(new CommandBinding(ScrollBar.LineDownCommand, LineCommandExecuted));
    }

    private void LineCommandExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        if (e.Command == ScrollBar.LineUpCommand)
        {
            if (this.VerticalOffset == 0)
                this.ScrollToEnd();
            else
                this.LineUp();
        }

        if (e.Command == ScrollBar.LineDownCommand)
        {
            if (this.VerticalOffset == this.ScrollableHeight)
                this.ScrollToTop();
            else
                this.LineDown();
        }
    }
}

ال انتقل إلى... و خط... الأساليب موجودة بالفعل على scrollview. جعل الترميز بسيط جدا. كل ما أفعله هنا هو التحقق من التعويض الحالي مقابل حدود المشاهد قبل التمرير.

الخطوة التالية هي إدراج الجديد scrollview. في قالب التحكم للتحكم الهدف، في هذه الحالة listBox.. وبعد إليك مقتطف XAML لإظهار هذا.

        <ControlTemplate x:Key="{x:Type ListBox}" TargetType="ListBox">
            ...
                <l:CyclicScrollViewer 
                    Padding="{TemplateBinding Control.Padding}" 
                    Focusable="False">
                    <ItemsPresenter ... />
                </l:CyclicScrollViewer>
            ...
        </ControlTemplate>

لقد نشرت تطبيق نموذج يستخدم هذا الرمز هنا. وبعد لا تحتوي هذه العينة على دعم لوحة المفاتيح، ولكن يمكن إضافةها ببساطة عن طريق تجاوز onkeydown. طريقة وتنفيذ المناسبة خط... أوامر. آمل أن يساعد هذا.

نصائح أخرى

هناك عدد من تطبيقات دائرة WPF المجانية والتجارية التي تفعل هذا. إلقاء نظرة على هذه الجولة http://mdavey.wordpress.com/2007/04/03/wpf-carousel/

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top