简体   繁体   English

列表框中的项目具有多个可选区域

[英]Listbox where items have more than one selectable region

I'm not sure about the best way to implement this in WPF, so I'll state my problem first. 我不确定在WPF中实现这个的最佳方法,所以我先说明我的问题。

I have a collection of frames. 我有一组帧。 Each frame has two images. 每帧有两个图像。 Let's say I have 10 frames giving a total of 20 images. 假设我有10帧,总共有20张图像。 I want to show the images at the bottom of the screen organized like a film strip - 2 rows and 10 columns. 我希望将屏幕底部的图像显示为电影条 - 2行10列。 When the user clicks on one of this images or uses the arrow, it should become selected and the selected image information will be used somewhere else in the application. 当用户单击其中一个图像或使用箭头时,它应该被选中,并且所选图像信息将在应用程序中的其他位置使用。

I've implemented it as a ListBox with ItemsSource bound to my viewmodel's Frames collection (an observablecollection). 我将它实现为ListBox,ItemsSource绑定到我的viewmodel的Frames集合(一个observablecollection)。 In the DataTemplate of the ListBox, I've created a grid with two rows, each one containing a Image control. 在ListBox的DataTemplate中,我创建了一个包含两行的网格,每一行都包含一个Image控件。 The one on row 0 is bound to TopImage (a property of my Frame class) and the bottom one is bound to BottomImage. 第0行的一个绑定到TopImage(我的Frame类的属性),底部绑定到BottomImage。

All this work, but the problem is that when I use the arrows, the whole frame (item) gets selected. 所有这些工作,但问题是当我使用箭头时,整个框架(项目)被选中。 How do I select each image in the datatemplate individually? 如何单独选择数据模板中的每个图像?

OR 要么

Is there a better way to implement this> 有没有更好的方法来实现这一点>

You have two problems: 你有两个问题:

  • You want to separate the selectability of the upper and lower images in a frame 您想要分离帧中上下图像的可选择性
  • You want the arrow keys to be able to navigate images in two dimensions 您希望箭头键能够以二维方式导航图像

If you did not have the arrow key requirement, then you could solve the first problem by nesting ListBox objects inside a parent ItemsControl . 如果您没有箭头键要求,那么您可以通过在父ItemsControl嵌套ListBox对象来解决第一个问题。 But then the arrows only do the right thing within a ListBox . 但是箭头只能在ListBox做正确的事情。 To address that requires a different approach. 要解决这个问题需要采用不同的方法

Here is a 2x2 grid data-bound to a four-element collection of images. 这是一个2x2网格数据绑定到四元素图像集合。 Here we use the little-used UniformGrid to cause the collection to wrap after so many columns. 在这里,我们使用很少使用的UniformGrid来使集合在这么多列之后进行包装。 Since we're using an ItemsControl , we lose automatic selection support but we get it back by making the Image control the content of a Button . 由于我们正在使用ItemsControl ,因此我们失去了自动选择支持,但我们通过将Image控件作为Button的内容来获取它。

<Grid>
    <Grid.Resources>
        <x:Array x:Type="sys:String" x:Key="sampleData">
            <sys:String>http://thecybershadow.net/misc/stackoverflow.png</sys:String>
            <sys:String>http://thecybershadow.net/misc/sourceforge.png</sys:String>
            <sys:String>http://thecybershadow.net/misc/stackoverflow.png</sys:String>
            <sys:String>http://thecybershadow.net/misc/sourceforge.png</sys:String>
        </x:Array>
    </Grid.Resources>
    <ItemsControl ItemsSource="{StaticResource sampleData}" Focusable="False">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid Columns="2"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Button>
                    <Button.Content>
                        <Image Source="{Binding}"/>
                    </Button.Content>
                </Button>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

The net effect is a 2x2 grid of images that you can freely arrow around between. 净效果是2x2网格的图像,您可以自由地在它们之间箭头。 You can use styling to make the button aspect less button-like without losing focusability. 您可以使用样式使按钮方面更像按钮,而不会失去可聚焦性。 So bind all twenty images to this view, first the top ten and then the bottom ten. 因此将所有二十个图像绑定到此视图,首先是前十个,然后是后十个。 You can also bind the column count from the same data source. 您还可以从同一数据源绑定列计数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM