How to get rid of box selection border of GridView UWP - c#

I have this gridview which works fine, but everytime I select an item I got this blue line around the item, how to remove it ?
<GridView Margin="5,15,0,0" x:Name="List" ItemsSource="{Binding}" SelectionChanged="List_SelectionChanged">
<GridView.ItemTemplate>
<DataTemplate>
<Grid Margin="11">
<StackPanel BorderBrush="Black" Orientation="Vertical">
<Image Width="150" Height="150" Source="{Binding Way}" />
</StackPanel>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid MaximumRowsOrColumns="2" Orientation="Horizontal" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>

The following fixed it for me:
<GridView.ItemContainerStyle>
<Style TargetType="GridViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="GridViewItem">
<ListViewItemPresenter
SelectedBackground="Transparent"
SelectedPointerOverBackground="Transparent"
PressedBackground="Transparent"
SelectedPressedBackground="Transparent"
/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GridView.ItemContainerStyle>

To remove the selection blue border of GridView, we can modify the template of GridView. To modify the template of GridViewItem, we can select the GridView in "Document Outline" and right click, then select "Edit Additional Templates"→ "Edit Generated Item Container (ItemContainerStyle)" → "Edit a Copy...".
In the Style, there is a ListViewItemPresenter in it.
When developing for Windows 10, use ListViewItemPresenter instead of GridViewItemPresenter in your item container style, both for ListView and for GridView.
For more info, see ListViewItemPresenter.
The color of blue line around the item is defined by SelectedBackground="{ThemeResource SystemControlHighlightAccentBrush}". We can set the SelectedBackground="Transparent", then there is no blue line around the item.

This is how I do it. Although it's not difficult to change the style, this requires about 99% less xaml (and a bit more code) to accomplish. You will have to remove the SelectionChanged event, specify the data type for your DataTemplate and add a Tapped event to each item.
<GridView SelectionMode="None" ItemsSource="{Binding}"
<GridView.ItemTemplate>
<DataTemplate x:DataType="YourType">
<Grid Tapped="Grid_Tapped_For_Every_Item">
...
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
...
</GridView>
in code file:
private void Grid_Tapped_For_Every_Item(object sender, TappedRoutedEventArgs e){
var g = (Grid) sender;
var myClass = (YourType)g.DataContext;
//Do whatever you were going to do in the SelectionChanged event
}

Related

Align GridView children to top of cell in UWP XAML

I am using GridView to display a grid of variable-height items, except each cell is aligned to the middle of the row.
My markup is
<GridView ItemsSource="{x:Bind Bounties}" SelectionMode="None" HorizontalAlignment="Left"
VerticalAlignment="Top" >
<GridView.ItemTemplate>
<DataTemplate x:DataType="local:Bounty">
<Grid>
<TextBlock Margin="12" MaxWidth="350" HorizontalAlignment="Left"
Text="{x:Bind ItemDefinition.DisplayProperties.Description}"
Style="{ThemeResource BodyTextBlockStyle}" TextWrapping="WrapWholeWords" />
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
How can I get each cell to align to the top left of its row?
The internal content of the GridViewItem is vertically centered, this is the default style, if you want to modify it, please try this:
<GridView ...>
<GridView.ItemContainerStyle>
<Style TargetType="GridViewItem">
<Setter Property="VerticalContentAlignment" Value="Top" />
</Style>
</GridView.ItemContainerStyle>
<GridView.ItemTemplate>
...
</GridView.ItemTemplate>
</GridView>
In addition, if you want to make the elements in the GridView adaptive height (the default is the height of the first element or the preset height as the height of all items), you can try StaggeredPanel in Windows Community Toolkit.
Thanks.

How do I get rid of unwanted padding in a Listview row

I am trying to figure out how to get rid of the padding below the text in a listview row:
And my markup for the Listview:
<ListView
ItemsSource ="{Binding AllowedApplicants}"
Height="250"
Width="219"
VerticalAlignment="Top"
Grid.Row="3"
Grid.Column="1"
Grid.ColumnSpan="2"
Margin="20,5"
BorderBrush="Bisque"
BorderThickness="2">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" Padding="5,5" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I can't figure out where this extra padding (red arrow) is coming from. The properties for the row's padding and margins defaults to zero all around. I added the five to keep the text off the borders. The list view row seems to have a default Height which cannot be adjusted.
Try adding
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="MinHeight" Value="0"/>
</Style>
</ListView.ItemContainerStyle>
to your List View or use Live Property Explorer and Live Visual Tree Viewer from Visual Studio and peek into the ListViewItem.

How can i make a ListView's items take up all the available height?

I am trying to make a column chart using WPF. For the Y axis I have a list of values which I would like to display on the control.
I have a ListView bound to the collection. How can I get the items to spread over the entire length of the list view rather then squish together at the top? And is the ListViewthe correct control to use for this?
Here is a sample of my Xaml:
<ListView Grid.Row="1" Grid.Column="1" Grid.RowSpan="2"
ItemsSource="{Binding YAxisValues}"
Background="Gray" VerticalContentAlignment="Stretch">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Path=Value}"
Foreground="White"
HorizontalAlignment="Left"
FontSize="12" Width="50"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
On the left is the list view as i currently have it.
On the right is my desired result.
How can I change my ListView to get there? Or do I need to use a different control entirely?
with UniformGrid used as ItemsPanel items get equal height
<ListView Name="Axis" Grid.Row="1" Grid.Column="1" Grid.RowSpan="2"
ItemsSource="{Binding YAxisValues}"
Background="Gray" VerticalContentAlignment="Stretch"
ScrollViewer.VerticalScrollBarVisibility="Disabled">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="1"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<!--no changes-->
</ListView.ItemTemplate>
</ListView>
there can be a issue when ListView doesn't have enough height for all elements. In that case I would put ListView into ViewBox
Set HorizontalContentAlignment to Stretch for items.
<ListView>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>

wpf combobox with custom itemtemplate text

I have ComboBox with custom ItemTemplate.
<ComboBox Height="20" Width="200"
SelectedItem="{Binding Path=SelectedDesign}"
ItemsSource="{Binding Path=Designs}" HorizontalAlignment="Left"
ScrollViewer.CanContentScroll="False">
<ComboBox.ItemTemplate>
<DataTemplate DataType="{x:Type formdesign:FormDesignContainer}">
<Rectangle Width="200" Height="100">
<Rectangle.Fill>
<ImageBrush ImageSource="{Binding Path=ImageThumb}" Stretch="Uniform" />
</Rectangle.Fill>
</Rectangle>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
This works well. However WPF tries to draw rectangle as Combobox Text. How can I set "text" for this template. By "text" I mean string or control which represent selected item and write into combobox when item is selected
In other words I'd like to do this:
But now I got this
Try setting SelectionBoxItemTemplate with a TextBlock.
Appears that SelectionBoxItemTemplate is read-only. So another approach is to override ItemContainerStyle.Template. Example
I found this solution by Ray Burns a good approach. You can define two DataTemplate one for Items in the drop down list and the other for the selected item which should be shown in the Combobox. The using a trigger and checking the visual tree it decide which one to use.
<Window.Resources>
<DataTemplate x:Key="NormalItemTemplate" ...>
...
</DataTemplate>
<DataTemplate x:Key="SelectionBoxTemplate" ...>
...
</DataTemplate>
<DataTemplate x:Key="CombinedTemplate">
<ContentPresenter x:Name="Presenter"
Content="{Binding}"
ContentTemplate="{StaticResource NormalItemTemplate}" />
<DataTemplate.Triggers>
<DataTrigger
Binding="{Binding RelativeSource={RelativeSource FindAncestor,ComboBoxItem,1}}"
Value="{x:Null}">
<Setter TargetName="Presenter" Property="ContentTemplate"
Value="{StaticResource SelectionBoxTemplate}" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</Window.Resources>
...
<ComboBox
ItemTemplate="{StaticResource CombinedTemplate}"
ItemsSource="..."/>
Add Textblock to the datatemplate and bind it
or add Contentpersenter on the rectangle
Edit:
it seems like i didn't got what you were tring to accomplish ,

Set focus in WPF Checkbox list

I have a WPF checkbox list containing car models. When I tab into the list the follwoing happens:
1) On first tab key press, the list item is selected
2) On second tab key press, the actual checkbox is selected allowing me to set the IsChecked state using the spacebar key.
Is it possible to automatically move the cursor to the checkbox when the list item is selected? (trying to avoid hitting tab twice)
XAML:
<ListBox ItemsSource="{Binding Cars}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Make}" IsChecked="{Binding IsChecked,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Try adding this to your ListBox:
<ListBox.ItemContainerStyle>
<Style TargetType="Control">
<Setter Property="Focusable" Value="False" />
</Style>
</ListBox.ItemContainerStyle>
Tried Dan's solution, it works but it moves the focus out of the ListBox control after selecting the first checkbox, I was looking for something which prevents the focus from being set to the selected listbox item while still allowing to tab through all the checkboxes in the list.
The following link helped me - Keyboard focus to list box items in WPF
I've made a few changes to have create border as well as the verticall scrollbar, complete XAML below:
<Border BorderThickness="0.8" BorderBrush="Gray">
<ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto" Margin="1">
<ItemsControl ItemsSource="{Binding Cars}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Make}" IsChecked="{Binding IsChecked,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</Border>

Categories

Resources