Style Listviews Selector.SelectedItem

I have ListView with SelectionMode=extended and style for ListViewItem thus:

MainWindow.xaml:

<Window x:Class="ListViewSelection.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <Style TargetType="{x:Type ListViewItem}"> <Style.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Aqua"/> </Style.Resources> <!--<Style.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="Background" Value="Aqua" /> </Trigger> </Style.Triggers>--> </Style> </Window.Resources> <StackPanel> <ListView Name="MyListView" ItemsSource="{Binding MyList}" SelectionChanged="SelectionChanged" SelectionMode="Extended" /> <Label Name="MyLabel" /> </StackPanel> </Window>

MainWindow.xaml.cs:

using System.Windows; using System.Windows.Controls; using System.Collections.ObjectModel; namespace ListViewSelection { public partial class MainWindow : Window { public ObservableCollection<string> MyList { get; set; } public MainWindow() { InitializeComponent(); this.DataContext = this; MyList = new ObservableCollection<string>(); MyList.Add("Jane"); MyList.Add("Paul"); MyList.Add("Sally"); MyList.Add("Ian"); } private void SelectionChanged(object sender, SelectionChangedEventArgs e) { MyLabel.Content = (sender as ListBox).SelectedItem; } } }

This sets the color fine for all selected items. But I need to also style the Selector.SelectedItem, which is the 'active' or first item in the selection. This is the one that the label displays.

Any help? Thanks!

-------------Problems Reply------------

You could expose IsFirstInSelection property in your ViewData class (I suppose you have it).

Then you could place DataTrigger for monitoring changes like this:

<Style.Triggers>
<DataTrigger Binding="{Binding IsFirstInSelection}">
<Setter Property="Background"
Value="Blue"/>
</DataTrigger>
</Style.Triggers>

Also in your case I'd recommend to avoid changing HighlightBrushKey, but achieve your goal via changing Background property. You could also expose IsSelected property in ViewData class, so it looks this:

public class ViewData: ObservableObject
{
private bool _isSelected;
public bool IsSelected
{
get
{
return this._isSelected;
}
set
{
if (this._isSelected != value)
{
this._isSelected = value;
this.OnPropertyChanged("IsSelected");
}
}
}

private bool _isFirstInSelection;
public bool IsFirstInSelection
{
get
{
return this._isFirstInSelection;
}
set
{
if (this._isFirstInSelection != value)
{
this._isFirstInSelection = value;
this.OnPropertyChanged("IsFirstInSelection");
}
}
}
}

And your style sets Background based on both properties:

<Style x:Key="TreeItemStyle"
TargetType="{x:Type TreeViewItem}">
<Setter Property="IsSelected"
Value="{Binding Path=IsSelected, Mode=TwoWay}"/>
<Setter Property="IsFirstInSelection"
Value="{Binding Path=IsFirstInSelection}"/>
<Setter Property="Background"
Value="Green"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected}"
Value="True">
<Setter Property="Background"
Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsFirstInSelection}">
<Setter Property="Background"
Value="Blue"/>
</DataTrigger>
</Style.Triggers>
</Style>

Of course in model or in view code behind you should monitor IsSelected property changed notifications and set/reset IsFirstInSelection according to selected items.

I hope you understand my idea.

------- EDITED --------

At first I continue to insist that you should develop proper ViewData class:

public class ViewData : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
var ev = this.PropertyChanged;
if (ev != null)
{
ev(this, new PropertyChangedEventArgs(propertyName));
}
}

public ViewData(string name)
{
this._personName = name;
}

private string _personName;
public string PersonName
{
get
{
return this._personName;
}
set
{
if (this._personName != value)
{
this._personName = value;
this.OnPropertyChanged("PersonName");
}
}
}

private bool _isFirstSelected;
public bool IsFirstSelected
{
get
{
return this._isFirstSelected;
}
set
{
if (this._isFirstSelected != value)
{
this._isFirstSelected = value;
this.OnPropertyChanged("IsFirstSelected");
}
}
}
}

During handling SelectionChanged event you should modify property IsFirstSelected like this:

public ObservableCollection<ViewData> MyList { get; set; }
private void SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var lb = sender as ListBox;
if (lb != null)
{
MyLabel.Content = lb.SelectedItem;
foreach (var item in this.MyList)
{
if (item == lb.SelectedItem)
{
item.IsFirstSelected = true;
}
else
{
item.IsFirstSelected = false;
}
}
}
}

But default ListViewItem template doesn't react on Background setters so you should override its template in style:

<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter Content="{Binding PersonName}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected"
Value="True">
<Setter Property="Background"
Value="Yellow"/>
</Trigger>

<DataTrigger Binding="{Binding IsFirstSelected}"
Value="True">
<Setter Property="Background"
Value="Aqua" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

If this solution isn't acceptable, please tell me. I've some ideas but have no enough time to check them in code right now.

Category:wpf Views:0 Time:2011-01-28

Related post

  • Problem setting ListView selector color when pressed 2010-09-28

    I have the following ListView, and have followed the answer from this SO Question w/ no luck: http://stackoverflow.com/questions/2038040/android-listview-selector-color <ListView android:layout_width="fill_parent" android:layout_height="fill_paren

  • How do I Style ListView Items? 2010-12-17

    I have a ListView that is data bound and I want to alter the font properties for each item. I'm not having much look finding the appropriate properties. The ListView is pretty simple so I don't expect it to be too difficult to change, I'm just not fi

  • Can't use default ListView selector 2011-03-19

    I'm developing a ListView app for Android. I want to use the default ListView selector, for example the one in Market when you touch an item. I don't get a highlight in my ListView, where's the way to correctly implement it? I've read about the "Touc

  • Transparent ListView selector does not appear 2011-05-26

    I have a custom ListView selector: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_enabled="false" android:state_focused="true" android:drawable="@drawabl

  • Help with listView and .SelectedItem in C# 2011-06-14

    Within my winform(C#) I have listView with two columns, which is used to display the results from searching an array, which is the basic details of an array entry(All data within the array is type string). I am able to add and remove items to the lis

  • Disable Android ListView selector 2011-06-15

    I am trying to disable the ListView selector on my ListView. I set the List Selector color to a transparent color. This seems to do the trick, but when I select certain text, they change colors. How can I make things work ok? --------------Solutions-

  • Windows 8 ListView SelectedIndex / SelectedItem (not scrolling) 2012-03-31

    I have a Windows 8 Metro app created in XAML, C# from the template. I need to navigate to the SplitPage and have an article selected. By default, the first article is selected (in the ListView and content shown on the right). I need to force the Spli

  • iPhone style listViews for Cocoa/Monomac 2012-04-11

    The iPhone has some really nice ways of creating listViews with custom cells. Is there any similar thing for CocoaOSx or Monomac? I am using both to create a set of apps and would like to create nice iPhone style listViews like those in the Twitter a

  • Default Style ListBox / ListView / ComboBox SelectedItem 2010-06-15

    I would like to know what setters I have to change in XAML to remove the default style when you select an item in any collection like(listview/listbox/combobox). By default it's blue,but I didn't want any visual aspect cause my listbox contains a sta

  • WPF ListView setting SelectedItem 2010-02-18

    I've tried to search for an answer to this but I'm not having any luck. Basically I have a listview that is bound to a collection returned from a view model. I bind the selected item of the list view to a property in my listview in order to perform v

  • Android: ListView Selector set to null? 2011-07-09

    I want to disable the Selector from my ListView. That's when I click any items there will be no indication that it has been clicked. I tried this in my ListView tag: android:listSelector="@null" and didn't work --------------Solutions------------- se

  • Android ListView Selector Color 2010-01-10

    Hi All, I have 2 questions regarding a ListView in Android: How can I get the color of the listview's focused row ? I tried to use the ListView.getSelector() method, which according to its documentation should give me what I'm looking for, but it's g

  • Overlap problem with Android ListView selector 2010-04-28

    I am trying to style my ListView with two 9-patch background images (16px * 9px), one dark image for default state and another green image for selected and pressed state. It works except for just one problem that when I select or press one list item,

  • C# Windows7/Vista style ListView 2010-12-16

    when I create a ListView in details view in VS2008, it creates a rather old looking listview. It doesnt have any of the fancy hover effects and cool blue border that (for example) the file explorer in WinVista and Win7 has. See image for example: So

  • ListView selector does not work when views inside listitem are clickable 2011-03-18

    I have a listview where views(ImageView and TextView) inside listitem are clickable. I have also set onClickListener on rowview inside adapter getView() to make whole row clickable. When I click on list item, I don't see any color change however it w

  • Problem creating a custom ListView selector 2011-05-25

    When I try clicking on a a listview item no selector is shown. Here is my ListView <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android

  • Android-Widget : Changing ListView selector color using RemoteView 2011-07-05

    Is it possible to set the selector color of listView present on the appWidget using remoteview. Basically I wanted to change its color according to the theme selected by user, so I can't define it in the layout xml. Please help --------------Solution

  • android: removing listview selector 2011-07-15

    I have changed the lsitview selector color to transparent. Even then when I click on the list item, the color of the text changes to dark grey.. is there anyway to handle this issue. I don't want the color to be changed upon item click .. -----------

  • Listview Binding SelectedItem Grids Image how to get 2012-05-02

    Hi need some hep with the below problem I am using a Listview which has a grid that contains an Image and its border .Chk the below Xaml Code <ListView x:Name="listview1" MinWidth="250" MaxWidth="250" SelectionChanged="listview1_SelectionChanged_1

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.117 (s). 11 q(s)