how to change selected item background based on page - xaml

I want to change selected item background color of my GridView based on pages. I define a Color property in each ViewModels, then assign the ViewModel to the view's DataContext. I edit the ItemContainerStyle in app.xaml and want to bind the ViewModel's color property to the selectionbackground, so that the background color of selected item is different in each views, but it doesn't work, I couldn't see the expected color. Anyone can help?

If we apply styles in the app.xaml page they will apply to all our application.
I think you can do this by defining your resources at page level using "UserControl.Resources" in your page.
<UserControl.Resources>
<Style TargetType="...">
...
</Style>
</UserControl.Resources>
You can even set key property in the style and apply to the control like bellow
<UserControl.Resources>
<Style x:Key="my_key" TargetType="...">
....
</Style>
</UserControl.Resources>

Related

Border element with a background hides the other element under it. (UWP)

I have a grid with multiple buttons as children. Now I want to give this Grid rows a background color. So I added a border with a background color like this
Border brd = new Border
{
Margin = new Thickness(0, 2, 0, 2),
Background = (SolidColorBrush) Application.Current.Resources["RedBrush"],
CornerRadius = new CornerRadius(22),
};
Grid.SetColumn(brd,startColumn);
Grid.SetColumnSpan(brd, 9- startColumn);
Grid.SetRow(brd, startRow);
GridMain.Children.Add(brd);
All looks good to me, But when this is rendered all buttons are hidden as the Background color of Border only is visible in the row. How can I overcome this?
Instead of adding the Border from code behind, If I added in Xaml all works fine (border background is visible as button background and button text is visible)
<Border Grid.Row="5" Grid.Column="1" Grid.ColumnSpan="8" Background="{StaticResource RedBrush}" CornerRadius="22" Margin="0 2 0 2" />
But for various reasons I wan the ability to add this border from code behind itself.
If you want to set the background color for Grid, you can directly use Grid.Background to set, without adding a new Border.
But if you need to add a Border as a background, please pay attention to the calling sequence in the code.
Generally speaking, the element added later will be placed on the top layer of the previous element, forming a visual effect of covering.
From your problem description. You can put the code that adds Border first.
Thanks.

"Shortcut" for Setting ListViewItem Selected Background

I'm using Anniversary Update (14393).
I can use this code to set ListViewItem Background.
<SolidColorBrush x:Name="ListViewItemBackground" Color="AntiqueWhite" />
Is it possible using this technique to set color for ListViewItem Selected/PointOver Background?
Yes, you can override the selected background brush for the ListView, so that it will use your color rather than the default. You do this by providing a ListView resource with the same key defined by the control.
<ListView>
<ListView.Resources>
<SolidColorBrush x:Key="ListViewItemBackgroundSelected" Color="Yellow"/>
<SolidColorBrush x:Key="ListViewItemForegroundSelected" Color="LimeGreen"/>
<SolidColorBrush x:Key="ListViewItemBackgroundSelectedPointerOver" Color="Blue"/>
</ListView.Resources>
</ListView>
UWP defines global brushes for all of its controls to make theming easy. By setting the resource within the ListView.Resources collection, these changes only affect this instance of the ListView.
If you want to set the same color scheme for the page or the entire app, you can override these brushes either in the Page or App resource dictionaries.

Numbering a ListView

I have a ListView whose Data Context is an ObservableCollection. I then used XAML to format the items and bind to their properties in the ListView in ListView.ItemTemplate.DataTemplate . Now, I want to add a TextBlock here to display the position of the item in the ListView through Binding in XAML. How to do that?
If I got your question right, you just need to add a TextBlock in each item DataTemplate with the index number inside the ListView, right?
If so, you can easily do that by doing something like this:
<DataTemplate x:Key="ItemTemplate">
<Grid>
<!--All your various UI elements here...-->
<!--Add a TextBlock with the formatting you want-->
<TextBlock Text="{Binding Position}"/>
</Grid>
</DataTemplate>
And in C#, add something like this after you've created your Source ObservableCollection, before assigning it to the Property inside your ViewModel:
for (int i = 0; i < myCollection.Count; i++)
{
myCollection[i].Position = Convert.ToString(i);
}
//And then, the usual
Source = myCollection
NB: I'm assuming you have a Property called Source in your ViewModel with INotifyPropertyChanged implemented. You will also have to modify the class you use inside your ObservableCollection and add the Position property of course :)

Horizontal long list selector xaml

I have items coming from server dynamically and I need to create an ItemTemplate for those items.
I looked for ScrollViewer but I couldn't find ItemTemplate or ItemsSource property.
Is there a way to list items in phone:longListSelector horizontally?
You can inside your Scrollviewer use a Listbox like this;
<ScrollViewer>
<ListBox ItemsSource="your source" ItemTemplate="your template" />
</ScrollViewer>

how to set a Viewmodel property when a listbox item is selected

I'm creating a spline designer which requires multiple spline parts.
It contains 2 views (2 UserControls).
The left one is an ItemsControl templated as a Canvas displaying the splines to edit.
The splines parts are UserControls as well.
The right one is a simple ListBox used to select a Spline part.
These two item container are bound to the same ObservableCollection in a ViewModel.
For now, I have a dependencyProperty in the SplinePartVM named IsSelected
What I exactly want to achieve is to modify the DependencyProperty of the SplinePartVM when the SelectedItem is set in the ListBox.
for example, i'd like to do something like this:
<Trigger Property="IsSelected" Value="True">
<Setter Property="{Binding IsSelected}"/>
</Trigger>
because a simple
<ListBox IsSelected="{Binding SelectedItem, Path=IsSelected, Mode=TwoWay}"/>
doesn't work.
I'm a bit lost here...
I found it.
I had to set IsSelected in the style of the ListBoxItem to make it work.