How about we write you a better one that is much easier to understand? Plus you can have any combination of highlight colors? I use this for a few of my apps. All it does is it binds the background color to the class as well. If it is selected it returns the highlight color of the class if not it returns the non highlight color.
Sample Data Point - as you can see you can set a highlight color and a no highlight color
public class sample_data : INotifyPropertyChanged
{
// Create the OnPropertyChanged method to raise the event
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
public sample_data(string name)
{
this.Name = name;
this.IsSelected = false;
this.NonHighlightColor = new SolidColorBrush(Colors.Transparent);
this.HighLightColor = new SolidColorBrush(Colors.Red);
}
public string Name { get; set; }
private bool _is_selected;
public bool IsSelected
{
get { return _is_selected; }
set
{
_is_selected = value;
OnPropertyChanged("HighlightBackgroundColor");
}
}
public SolidColorBrush HighlightBackgroundColor
{
get { if (IsSelected) return HighLightColor; else return NonHighlightColor; }
}
private SolidColorBrush HighLightColor{ get; set; }
private SolidColorBrush NonHighlightColor { get; set; }
}
Lets create the ObservableCollection and set the LongListSelector's ItemSource.
private ObservableCollection<sample_data> CreateSampleData()
{
ObservableCollection<sample_data> sd = new ObservableCollection<sample_data>();
sd.Add(new sample_data("Bob"));
sd.Add(new sample_data("Dan"));
sd.Add(new sample_data("Kate"));
sd.Add(new sample_data("Bart"));
sd.Add(new sample_data("Sanders"));
sd.Add(new sample_data("Dog"));
return sd;
}
// Constructor
public MainPage()
{
InitializeComponent();
mylonglist.ItemsSource = CreateSampleData();
}
Now for the XAML
<phone:LongListSelector x:Name="mylonglist" SelectionChanged="mylonglist_SelectionChanged">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Background="{Binding HighlightBackgroundColor}" Height="100">
<TextBlock Text="{Binding Name}"></TextBlock>
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
Code for the Selection Change
private void mylonglist_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
LongListSelector ls = sender as LongListSelector;
sample_data selected_item = ls.SelectedItem as sample_data;
// unselected the previous selections
foreach (sample_data sd in ls.ItemsSource)
{
if (sd != selected_item)
{
sd.IsSelected = false;
}
}
// set the selected item (this will cause the background color to change)
selected_item.IsSelected = true;
}
catch (Exception ex)
{
string error = ex.Message;
}
}
There you have it, now you can highlight with any colors and with custom colors for each item without messing with the messy VisualState Manager.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…