I'm working on a C# project for Windows Phone 8.1, and I can't believe that I've already wasted almost a day looking for a solution to such a trivial problem:
I have a page defined with XAML, on that page I have a ListView. At some point, I want one of the list view items to become selected, so I call myListView.SelectedIndex = whatever. Now I want that item to be distinguished visually from other items, for example, have its text drawn with a different color. How do I do that? Here are the relevant parts of code:
<Page.Resources>
<DataTemplate x:Key="myListItemTemplate">
<TextBlock
Text="{Binding displayName}"
Style="{ThemeResource ListViewItemTextBlockStyle}"
/>
</DataTemplate>
</Page.Resources>
<ListView
x:Name="myListView"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource myListItemTemplate}"
>
</ListView>
Is it possible with XAML alone? Or can in be done in the C# code, just when I set myListView.SelectedIndex value?
Thanks!
You need to create a style for your ListBoxItem and use storyboards.
Here is a sample :
And the definition of the list view :
You just need to add the following to App.xaml
</Application.Resources> <SolidColorBrush x:Key="ListViewItemSelectedBackgroundThemeBrush" Color="#92D050" /> <Application.Resources>
The "hex-Color" will be the selected item color in a ListView at application scope
K, Andrei I think the solutions provided are quite good, it just buggy. Here is mine.
XAML : Pay attention to the SelectedUnfocused
C# (A sample model)
Screenshot of it running:
Yes it is, you need to set a style with a trigger on the selected property. Its toughto yype in code on my phone but a quick google will show you a ton of examples or here:ListBox Style Selected item on windows phone
Try this, Hope it may help you.
You can add to your class a boolean variable IsSelected, and convert it to a bg color. For example:
|