tags:

views:

78

answers:

1

I have a WPF ListView , where I bind an observable collection, and below is the code

         <ListView Name="WOListView" IsSynchronizedWithCurrentItem="True" 
                          DataContext="{Binding  AllItems}" 
                          ItemsSource="{Binding }"
                          SnapsToDevicePixels="True"   Grid.IsSharedSizeScope="True" 
                          customEvents:DoubleClickEvent.HandleDoubleClick="true"
                          customEvents:DoubleClickEvent.TheCommandToRun="{Binding Path=ItemCommand}" 
                          BorderThickness="0" >

Here I have two issues

1) Dont know why, i always get an item selected when the load the list, and it is always the 1st item the the collection I bind.

2) The selected item is normally show in blue color by default, but when i click out side the listview,rather than deselecting the item.it shows the seleceted item as grey in color

A: 

For 1): This is due to the IsSynchronizedWithCurrentItem="True". If you want use the IsSynchronizedWithCurrentItem-feature, you propably want to influence the CurrentItem. Use the following code to do so...

var dv = CollectionViewSource.GetDefaultView(yourObservableCollection);
dv.MoveCurrentTo( /* here your desired selection */ );

... see here for more information.

For 2): this is the default behaviour of ListView (and other ListControls). If the list-control does not have anymore the focus, the selected items are showed in gray. This is done so to visualize which control has the focus (and which control has not).

If you want to change the selection-behaviour anyhow, put the following markup into the ListView

<ListView.Resources>
    <Style TargetType="ListViewItem">
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Green"/>
        </Style.Resources>
    </Style>
</ListView.Resources>

Change the colors as you wish. But as I mentioned, the default behaviour is for showing the user which control has been selected and if you change this behaviour, some users may dislike...

HCL
@HCL the 1st solution really wotked great for me, Thanks for that.Is there any get around for the second one.
crazy9
@crazy9, see my update
HCL
oo ya that worked for me for the second question.Thanks for the code, but for the first question if i remove Issyndronisewithcurrentitem=true, that is not updating the list which are made later.. and many of the functionality which worked before ..stopped working
crazy9
@crazy: I have added a new section that handles the CurrentItem.
HCL
Thanks a lot, will try this solution
crazy9
@HCL Sorry for the late reply, this really worked great for me.
crazy9