Search code examples
c#wpfxamlcontrolsselectable

Selectable controls in WPF


Is there a general mechanism in WPF that allows for any or almost any control to be selected? For instance, you could have some Image controls on a WrapPanel defined in XAML, and you need to be able to select them either one by one, or by dragging over with the mouse to mark multiple.

In terms of code, I can imagine the following:

<WrapPanel> 
    <Image Source="{StaticResource ResourceKey=pic1}" />
    <Image Source="{StaticResource ResourceKey=pic2}" />
    <Image Source="{StaticResource ResourceKey=pic3}" />
    <Image Source="{StaticResource ResourceKey=pic4}" />
    <Image Source="{StaticResource ResourceKey=pic5}" />
</WrapPanel>

Solution

  • Yes, its called a ListView.

    If you need the look of a WrapPanel, just set the ItemsPanel property like so:

    <ListView>
       <ListView.ItemsPanel>
            <ItemsPanelTemplate>
               <WrapPanel/>
            </ItemsPanelTemplate>
       </ListView.ItemsPanel>
    </ListView>
    

    Documentation on MSDN.

    You would need to make the Image objects part of the ItemTemplate in your example. You can manually define the Items collection or use a CompositeCollection as the items source as well.