0

There are basically two things I am trying to activate or implement:

1. Clearing selection when background is clicked:

Typically, when one clicks in a blank area of a list view type control, such as windows explorer, any selected items become unselected. This is not happening for me in either multiple or extended selection mode. Do I have to manually handle the mouse click event to clear the selection, or is it perhaps not behaving as expected because I've applied a background to the control?

2. Selection rectangle with automatic scrolling:

Before porting my application to WPF, the standard WinForms listview allowed me to drag a selection rectangle and it would select any items it intersected. If there were items scrolled out of view in any direction, dragging in that direction would result in the control automatically scrolling into that area as I dragged the mouse, so I could selected items that are out of view. Does the WPF ListView implement this feature, or am I going to have to implement it myself? Someone posted a non-trivial implementation involving hittests in the comments on this page (http://social.msdn.microsoft.com/Forums/vstudio/en-US/191af722-e32b-4e6d-a00b-9ad2b53ea3b9/listview-dragging-a-selection-box-around-items?forum=wpf), but it doesn't even support the autoscrolling and I'm having a hard time believing Microsoft just left this feature out.

Triynko
  • 18,766
  • 21
  • 107
  • 173
  • Similar question was asked here: http://stackoverflow.com/questions/1838163/click-and-drag-selection-box-in-wpf, but neither the posted implementation nor another answer's linked CodeProject article implements the auto scrolling feature while dragging. – Triynko Feb 12 '14 at 20:29
  • What version of .NET? Selected, Not Focused is a highlight by default. – paparazzo Feb 12 '14 at 20:29
  • .NET 4.5, but I'm not sure what you're talking about, since there is no default appearance in WPF; I had to set everything, even basic selection and hover appearances with styles and triggers. – Triynko Feb 12 '14 at 20:52
  • Really? So the property SelectedItem did nothing? You had to use triggers for basic selection? Maybe you need to catch up with WPF. – paparazzo Feb 12 '14 at 21:38
  • Not for selection, for the visuals. When using a control template, for example to have each item display an image and a file name under it, clicking an item will select it, but it will not draw a border around it or otherwise alter its appearance. You must do that with triggers attached to the control template's IsSelected property or IsMouseOver that will explicitly alter the border color or background for that state. – Triynko Feb 13 '14 at 16:51
  • The format of SO is one question at time. Pick one. – paparazzo Feb 14 '14 at 01:33

1 Answers1

-2

Really ListView has no default appearance and you had to do set even basic selection with trigger.
Wow, this colors and displays the SelectedIndex without a single style or trigger.
All in XAML

<Window.Resources>
    <sys:String x:Key="MyString">Hello</sys:String>
    <x:Array x:Key="MyStringArray" Type="sys:String">
        <sys:String>Hello</sys:String>
        <sys:String>World</sys:String>
        <sys:String>Continent</sys:String>
        <sys:String>Universe</sys:String>
    </x:Array>
</Window.Resources>
<Grid>
    <StackPanel Orientation="Vertical">
        <ListView ItemsSource="{StaticResource MyStringArray}" x:Name="lv" SelectionMode="Single"  LostFocus="lv_LostFocus">
        </ListView>
        <TextBlock Text="{Binding ElementName=lv, Path=SelectedIndex}" />
        <Button Content="Take Focus"/>
    </StackPanel>
</Grid>

    private void lv_LostFocus(object sender, RoutedEventArgs e)
    {
        lv.SelectedIndex = -1;
    }
paparazzo
  • 44,497
  • 23
  • 105
  • 176
  • 1
    how does this answer either of OP's questions? – Sten Petrov Feb 12 '14 at 22:13
  • @StenPetrov It address the assertion that he had to handle everything even basic selection with triggers. Wow maybe a simple LostFous event might be the trick. – paparazzo Feb 12 '14 at 22:27
  • I think you've misunderstood what I was trying to say. Of course if you just stick a string array in it, it has a default appearance, but if you want to use the control to its full potential and display more than a string array, then you have to set up a data template with your own controls. When you do so, a listview does not automatically add colors or borders when you hover or select items. It still selects them under the hood, but it doesn't alter the visuals in any way. In other words, by default, it will not magically add a selection border or highlight to a control template. – Triynko Feb 13 '14 at 17:02
  • It also does not support a selection rectangle like the old control used to do. Some have called it a very "over-engineered", stripped down control that lacks basic features that the winforms listview had. On the other hand, there's no denying that it's far more flexible and can do much more... it just requires more work to do (for some things, and less for others), and I don't mind that at all. I'm just looking for help implementing those missing features. – Triynko Feb 13 '14 at 17:03
  • But you said it there is no default appearance. Does the LostFocus event answer #1. Post your XAML - it appears you are thinking Forms and WPF is different. Styling is not limited to string array. Why are you manually selecting? – paparazzo Feb 13 '14 at 17:40
  • Does LostFocus SelectedIndex = -1; answer question one or not? – paparazzo Feb 13 '14 at 23:40