开发者

ListView Item Coming default selected with the first item in the list

开发者 https://www.devze.com 2023-01-09 22:33 出处:网络
I have a WPF ListView , where I bind an observable collection, and below is the code <ListView Name=\"W开发者_开发百科OListView\" IsSynchronizedWithCurrentItem=\"True\"

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

         <ListView Name="W开发者_开发百科OListView" 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


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...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号