在ListBox项目内按ScrollViewer不会选择该项目

问题描述 投票:0回答:1

我给了一个列出项目的ListBox。这些项目的大小可能有所不同,但是遵循相同的模式:

图片.......元数据

ID

<ListBox Name="View"  Grid.Row="2" ItemsSource="{Binding Human, ElementName=uc}"
            SelectedItem="{Binding SelectedHuman, ElementName=uc}"
            MouseDoubleClick="OnSubjectEditClick"
            VerticalContentAlignment="Center" 
            VirtualizingPanel.IsVirtualizing="True"
            VirtualizingPanel.ScrollUnit="Pixel"
            ScrollViewer.CanContentScroll="True"
            Grid.IsSharedSizeScope="True"
            ScrollViewer.HorizontalScrollBarVisibility="Disabled">

            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemTemplate >
                <DataTemplate >
                    <Border BorderBrush="#565656" BorderThickness="1" Margin="10" Padding="10">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition MaxHeight="300" />
                                <RowDefinition MaxHeight="50" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="auto" MinWidth="300" MaxWidth="500" SharedSizeGroup="col1"/>
                                <ColumnDefinition  MaxWidth="500" Width="*" SharedSizeGroup="col2"/>
                            </Grid.ColumnDefinitions>

                            <Image Grid.Row="0" Grid.Column="0" MaxHeight="300" MaxWidth="300" Source="{Binding Thumb}"/>
                            <TextBlock Grid.Column="0" Grid.Row="1" Text="{Binding Name}" FontWeight="Bold" TextAlignment="Center"/>
                            <local:MetadataView Grid.Column="1" Grid.RowSpan="2" Metadata="{Binding Metadata}" HorizontalAlignment="Stretch" VerticalAlignment="Center" IsEdit="False"  />
                        </Grid>

                    </Border>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

Metadata .xaml中的项目看起来像这样。包含实际元数据的StackPanel由“ DisplayMode”中的代码自动生成:

<Grid VerticalAlignment="Center" HorizontalAlignment="Stretch">
    <ScrollViewer  VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" CanContentScroll="False" Height="300" >
            <StackPanel   x:Name="DisplayMode" VerticalAlignment="Center" Grid.IsSharedSizeScope="True" >
            ........... 
            <StackPanel/> //AutoGenerated
            ............
            </StackPanel>
    </ScrollViewer>
</Grid>

我面临的问题是事实,我需要能够在ListBox中选择一个项目。但是,通过在Metadata.xaml中添加ScrollViewer,似乎我重新路由了选择器,因此当我按包含元数据的ListBox的一部分时,它试图在Metadata ScrollViewer中而不是ListBox中选择一个项目。如果我按缩略图,甚至右键单击ListBox的任何部分,似乎都可以。

c# listbox scrollviewer
1个回答
0
投票

[已解决]在ScrollViewer中使用可用选项后,我偶然发现了Focusable。将其设置为False可以达到目的。我希望它将对以后的人有所帮助。

<Grid VerticalAlignment="Center" HorizontalAlignment="Stretch">
    <ScrollViewer  VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" CanContentScroll="False" Height="300" Focusable="False" >
        <StackPanel   x:Name="DisplayMode" VerticalAlignment="Center" Grid.IsSharedSizeScope="True" >

        </StackPanel>
    </ScrollViewer>
</Grid>
© www.soinside.com 2019 - 2024. All rights reserved.