Cannot select ListViewItem because a rectangle is in the way - c#

I have a ListView bound to an ObservableCollection and when items are added to the ListView, if you hover over a ListViewItem, you will see that not only the ListViewItem itself if highlighted, but there is also another slightly-smaller, darker-blue rectangle that appears. Now this slightly smaller rectangle prevents you from being able to select the ListViewItem because it is on-top of the ListViewItem.
Why is it there and how do I get rid of it?
<Grid Margin="0, 25, 0, 22" Grid.Column="0">
<ScrollViewer ScrollViewer.CanContentScroll="True" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
<ListView x:Name="list" ItemsSource="{Binding Source=Message}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderBrush="{x:Null}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListView.ItemTemplate>
<DataTemplate>
<ListViewItem HorizontalAlignment="Stretch" Height="50">
<StackPanel Orientation="Vertical" HorizontalAlignment="Stretch">
<TextBlock Text="{Binding Sender}" HorizontalAlignment="Stretch" TextTrimming="WordEllipsis" TextWrapping="Wrap" FontWeight="SemiBold" />
<TextBlock Text="{Binding Subject}" HorizontalAlignment="Stretch" TextTrimming="WordEllipsis" TextWrapping="Wrap" />
</StackPanel>
</ListViewItem>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ScrollViewer>
</Grid>

You have a ListViewItem in your ListViewItem. The ListView will automatically wrap it's items in a container so you don't have to do that. Remove the ListViewItem from your DataTemplate.

Remove ListviewItem from DataTemplate. ListView will wrap whatever is in your DataTemplate in ListViewItem. If you have something to set against ListViewItem do it through ItemContainerStyle
<ListView x:Name="list" ItemsSource="{Binding Source=Message}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderBrush="{x:Null}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" HorizontalAlignment="Stretch">
<TextBlock Text="{Binding Sender}" HorizontalAlignment="Stretch" TextTrimming="WordEllipsis" TextWrapping="Wrap" FontWeight="SemiBold" />
<TextBlock Text="{Binding Subject}" HorizontalAlignment="Stretch" TextTrimming="WordEllipsis" TextWrapping="Wrap" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="Height" Value="50"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>

Related

WPF Textsearch item in Listbox created in MVVM

I would like to search items in my ListBox by typing. I found it can be done by TextSearch Property.
ListBox XAML:
<ListBox x:Name="SzablonyBox" HorizontalAlignment="Center"
HorizontalContentAlignment="Center" VerticalAlignment="Center"
MinWidth="100" MinHeight="100" BorderBrush="{x:Null}"
Background="{x:Null}" TextSearch.TextPath="Text" IsTextSearchEnabled="True">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<EventSetter Event="PreviewKeyDown" Handler="ListBoxItem_PreviewKeyDown"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanel>
<TextBlock Text="{Binding nazwa}" FontWeight="Bold"
FontSize="15" Height="35"
VerticalAlignment="Center" HorizontalAlignment="Center"
TextAlignment="Justify"/>
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
How to set TextSearch.TextPath="Text" to look after ListBox items (TextBlock text) ?

Defining a border for ListView's item Template

Here is the XAML code for the ListView.
<ListView VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
x:Name="listView1">
<ListView.ItemTemplate>
<DataTemplate>
<Border HorizontalAlignment="Stretch"
BorderThickness="1"
BorderBrush="LightBlue">
<StackPanel Orientation="Vertical">
<Label BorderThickness="0,0,0,1"
BorderBrush="#BABABA"
HorizontalAlignment="Stretch">Keywords</Label>
<TextBlock Text="{Binding keywords}"
Foreground="#3399AA"
HorizontalAlignment="Stretch" />
<Label BorderThickness="0,0,0,1"
BorderBrush="#BABABA">Content</Label>
<TextBlock MaxHeight="50"
Text="{Binding contentAsString}"
TextWrapping="Wrap"
Foreground="Black"
Height="auto"
Width="auto" />
</StackPanel>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
And here is how it looks
No matter what I've tried I couldn't create a border for the whole listview item. How can we do that?
Try with Stretch
<ListView>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>
. . .
<Viewbox Stretch="Fill"/>

StackPanel ignore HorizontalAlignment issue XAML

I have issue, i just try to make chat window with messages(From right incoming and from left sended out) but seems like stackpanel ignore HorizontalAlignment. Here's my code
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<StackPanel HorizontalAlignment="Left">
<TextBlock TextWrapping="Wrap"
FontSize="22"
Text="{Binding MessageTextIn}"
/>
<TextBlock
FontSize="10"
Text="{Binding MessageTimeIn}" Padding="10"
/>
</StackPanel>
<StackPanel HorizontalAlignment="Right">
<TextBlock TextWrapping="Wrap"
FontSize="22"
Text="{Binding MessageTextOut}"
/>
<TextBlock
FontSize="10"
Text="{Binding MessageTimeOut}" Padding="10"
/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Seems like i do something wrong but what exactly?
Thanks for any help!
Sorry if i ask something stupid.
That is because your ItemContainer only has a right alignment (by default).
Set the ItemContainerStyle of your Listview:
<ListView>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
</ListView>

PivotItems do not resize after binding

I am binding a PivotItems but somehow the items are not resized properly, according to my margin declaration. Why is that?
EDIT: here is my new code and it still has gaps..
<Pivot x:Name="TpsSegmentsPivot" Title="Locator" Foreground="#FF888888" Style="{StaticResource PivotStyle1}" SelectionChanged="Pivot_SelectionChanged" Margin="0" Grid.Row="1" ItemTemplate="{StaticResource TpTemplate}" ItemsSource="{Binding DataSource}">
<Pivot.HeaderTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding id}" Margin="0, 16, 0, 0" Foreground="#FF888888" FontSize="32" FontFamily="Segoe WP" FontWeight="Light"/>
</Grid>
</DataTemplate>
</Pivot.HeaderTemplate>
</Pivot>
This is the template:
<DataTemplate x:Key="TpTemplate">
<ListBox Background="Black"
ItemsSource="{Binding Seg}"
ItemTemplate="{StaticResource SectionUCTemplate}"
HorizontalAlignment="Stretch"
VerticalAlignment="Top">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
</DataTemplate>
Here is a screenshot with the red pivotItem..
I think the issue with the extra space is the Piviot Item you are using inside the DataTemplate tag. remove it and bind the header to a header template. I've used data binding on Pivots before and never seen this issue.
From one of my released apps:
<controls:Pivot Title="CAMPING CHECKLIST" ItemsSource="{Binding FilteredCategories}" Name="MainPivot">
<controls:Pivot.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</controls:Pivot.HeaderTemplate>
<controls:Pivot.ItemTemplate>
<DataTemplate>
<!-- Your pivot item content here -->
</DataTemplate>
</controls:Pivot.ItemTemplate>
</controls:Pivot>
I believe the PivotItem in a PivotItem is the root of your issue.
your code should look similar to:
<controls:Pivot ItemsSource="{Binding tripTypeViewModel.TripTypeViewModelDataSource}" x:Name="TripsSegmentsPivot" Title=" " Foreground="#FF888888" Style="{StaticResource PivotStyle1}" SelectionChanged="Pivot_SelectionChanged" Grid.Row="1">
<controls:Pivot.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding DataContext.tripTypeViewModel.HeaderText, ElementName=TripsSegmentsPivot}" Margin="0, 16, 0, 0" Foreground="#FF888888" FontSize="32" FontFamily="Segoe WP" FontWeight="Light"/>
</DataTemplate>
</controls:Pivot.HeaderTemplate>
<controls:Pivot.ItemTemplate>
<DataTemplate>
<ListBox x:Name="ItemsLB" ItemsSource="{Binding DataContext.tripTypeViewModel.Segment, ElementName=TripsSegmentsPivot}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Transparent">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<local:SectionUC HorizontalAlignment="Stretch" Grid.Row="1" VerticalAlignment="Top"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
</controls:Pivot.ItemTemplate>
</controls:Pivot>
The first image is how I describe it, the second is how you currently have it :
This is the standard behaviour for PivotItems since they are always displayed with a margin. You should set a negative margin:
margin="-20,0,-20,0"

Disabled Items Showing in Listbox

I have a ListBox that is bound to a TabControl. Some of the TabItems inside this (TabControl) are hidden/disabled then become visible/enabled when I open a file.
My issue is that these hidden/disabled items are still visible in my ListBox. Can someone help me with regards as to why this is happening?
TabControl XAML
<TabControl Height="Auto" x:Name="tabControl" Width="Auto"
Padding="0" Margin="3" DataContext="{Binding}">
<TabItem Header="StartPage" x:Name="StartTab" Foreground="White" Height="25">
</TabItem>
<TabItem Header="DragDrop" x:Name="DragDropTab" Foreground="White"
Height="25" IsEnabled="False" Visibility="Hidden">
<Image Height="Auto" x:Name="DragImage" Stretch="Fill" Width="Auto" />
</TabItem>
<TabItem Header="Text" x:Name="TextTab" Foreground="White"
Height="25" IsEnabled="False" Visibility="Hidden" >
<Grid>
<cbox:SyntaxHighlightBox Height="Auto" x:Name="HighlightText"
Width="Auto" Text="" AcceptsTab="True" AcceptsReturn="True"
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Visible" />
</Grid>
</TabItem>
</TabControl>
ListBox XAML
<ListBox ItemsSource="{Binding Items, ElementName=tabControl}"
x:Name="ShowOpenTabs" >
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}"
BasedOn="{StaticResource {x:Type ListBoxItem}}">
<EventSetter Event="MouseDoubleClick" Handler="OpenOnClick"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Header}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
You need to bind the ListBox item visibility to the tabcontrol item visibility
<DataTemplate>
<TextBlock Text="{Binding Header}" Visibility="{Binding Path=Visibility}"/>
</DataTemplate>

Categories