Scrolling on an ItemsControl - xaml

I have created info inside a PivotItem but for some reason the items do not scroll, I've tried scrolling but it creates a sort of compressed effect and not scrolling down.
I have tried wrapping it with a ScrollViewer and also tried
<ItemsControl.Template>
<ControlTemplate>
<ScrollViewer VerticalScrollBarVisibility="Visible" VerticalScrollMode="Enabled">
<ItemsPresenter />
</ScrollViewer>
</ControlTemplate>
</ItemsControl.Template>
With no luck - can anyone identify what could be wrong?
<PivotItem Header="unread">
<ItemsControl ItemsSource="{Binding Categories}" >
<ItemsControl.Template>
<ControlTemplate>
<ScrollViewer VerticalScrollBarVisibility="Visible" VerticalScrollMode="Enabled" BringIntoViewOnFocusChange="True">
<ItemsPresenter />
</ScrollViewer>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemTemplate>
<DataTemplate>
// ---
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</PivotItem>

Just put the ScrollViewer around the ItemsControl, not inside it. Something like this:
<Pivot TabNavigation="Once">
<PivotItem Header="unread">
<ScrollViewer>
<ItemsControl ItemsSource="{Binding Categories}">
//Some ItemsControl properties and stuff
</ItemsControl>
</ScrollViewer>
</PivotItem>
</Pivot>
Solved
For everyone reading this - the problem was that the Pivot was in a StackPanel. And ScrollViewers do not work inside of StackPanels, because these panels have unlimited height (or width, depending on the Orientation), and then there's nothing to scroll as everything fits. You could use a StackPanel inside a ScrollViewer, though.

Related

UWP XAML Binding to another object is not working

I'm trying to bind TextBlock Width to another object's Width.
It is not working, TextBlock Width stays as the Text length, and not as "BitsListView" Width.
An interesting thing is, when I edit the "Width" of TextBlock while debugging, the binding is working OK.
<StackPanel >
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{x:Bind name}" Width="{Binding ElementName=BitsListView, Path=ActualWidth }"/>
</StackPanel>
<ListBox x:Name="BitsListView" ItemsSource="{x:Bind BitsList, Mode=TwoWay}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</StackPanel>
Any ideas?
UWP XAML Binding to another object is not working
The problem is that when set TextBlock root panel Orientation property as Horizontal, the width of content will be fixed. So, if you want to make Binding work, please remove Orientation property like the following.
<StackPanel>
<Border BorderBrush="Red" BorderThickness="2" HorizontalAlignment="Stretch" Margin="0,0,0,0">
<TextBlock Name="TestBlock" Text="Test input some" Width="{Binding ElementName=BitsListView, Path=ActualWidth}"/>
</Border>
</StackPanel>

Horizontally Positioning Controls inside ListBox

I am making a UWP app and trying to place two TextBlock inside a ListBoxItem. HorizontalAlignment property doesn't seem to work.
I am trying to align the first TextBlock to the left and the second TextBlock to the right. Currently I am trying using Grids. Here's my XAML:
<Pivot.ItemTemplate>
<DataTemplate>
<ListBox ItemsSource="{Binding List}"
SelectionMode="Multiple"
ScrollViewer.HorizontalScrollMode="Disabled">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="9*"></ColumnDefinition>
<ColumnDefinition Width="1*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0"
Text="{Binding read}"
HorizontalAlignment="Left"
VerticalAlignment="Center"/>
<TextBlock Text="{Binding num}"
Grid.Column="1"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
</Pivot.ItemTemplate>
A couple of things you need to do here:
First, you need to stretch the alignment of the ListBoxItem, not the ListBox itself.
<ListBox>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
Second, you should change Width="1*" to Width="Auto" otherwise the num TextBlock might get truncated. Then you can remove Width="9*" and add TextWrapping="Wrap" to the read TextBlock so if text will go to the next line if it's too long. You can safely remove HorizontalAlignment="Left" too.
Try to set the HorizontalContentAlignment property to stretch in the ListBox:
<ListBox ItemsSource="{Binding List}"
HorizontalContentAlignment="Stretch"
SelectionMode="Multiple"
ScrollViewer.HorizontalScrollMode="Disabled">
By default the HorizontalContentAlignment is set to left, and your listItem will not stretch to use all the available space, and that's why it's content will not be alligned properly to the right.

How to make a Stack Panel Visible based on the Tapped Event of a ListView in Windows Phone 8.1?

In my application, I am having a ListView. ListView lists a set of images.
So when the application is running, and when that page is loaded, a list of images are shown.
<ListView ItemsSource="{Binding imageLists}" Background="Red" Tapped="ListView_Tapped">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Background="Green">
<Image Source="{Binding imagePath}" CacheMode="BitmapCache" Stretch="UniformToFill"/>
<StackPanel Name="imageTitle" Visibility="Collapsed" Background="Blue"
HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Foreground="White" Text="Dumy Image Title" FontSize="10"/>
</StackPanel>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
As you can see from the code, I have one image & stackpanel inside the listview. And the stackpanel's visibility has been set to collapsed for convenience.
The StackPanel imageTitle resides inside the ListView. Stackpanel contains a TextBlock housing the images name. For now it's dumy text.
On Tapping any image in the list, I am trying to make the stackPanel visible.
The Code Behind:
private void ListView_Tapped(object sender, TappedRoutedEventArgs e)
{
imageTitle.Visibility = Windows.UI.Xaml.Visibility.Visible;
}
Since the stackpanel is inside the listview & I am trying to make it visible on the tap event of the listview, I am not able to acheive the needed result. I know my code is wrong.
If I specify the stackpanel outside the listview, I can make it visible using the code I gave inside the ListView_Tapped function. But even in that case, I need to show the stackpanel (name of the image I clicked) inside the listview item (image I clicked).
Any help??
Can this be achieved using only XAML?
Here's a pure xaml way.
Rather than changing the Visibility of the imageTitle (not a great UX), let's change its Opacity to make its appearing more interesting.
First we need to create a storyboard inside this data template. This storyboard will fade in the imageTitle in 400ms.
And then we drag a ControlStoryboard behavior from Expression Blend's Asset panel onto the top level Grid. Basically we want the storyboard to fire off when this Grid is tapped.
Please see below code for reference.
<DataTemplate x:Key="GroupTemplate">
<Grid Background="Green">
<Grid.Resources>
<Storyboard x:Name="ShowImageTitleStoryboard">
<DoubleAnimation Duration="0:0:0.4" To="1" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="imageTitle"/>
</Storyboard>
</Grid.Resources>
<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior EventName="Tapped">
<Media:ControlStoryboardAction Storyboard="{StaticResource ShowImageTitleStoryboard}"/>
</Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
<Image Source="{Binding Image}" CacheMode="BitmapCache" Stretch="UniformToFill"/>
<StackPanel x:Name="imageTitle" Background="Blue"
HorizontalAlignment="Center" VerticalAlignment="Center" Opacity="0">
<TextBlock Foreground="White" Text="Dumy Image Title" FontSize="10"/>
</StackPanel>
</Grid>
</DataTemplate>
Apart from JustinXL's answer it can also be done by using ChangePropertyAction. Also pure XAML:
<DataTemplate>
<Grid Background="Green">
<Image Source="{Binding imagePath}" CacheMode="BitmapCache" Stretch="UniformToFill">
<i:Interaction.Behaviors>
<ic:EventTriggerBehavior EventName="Tapped">
<ic:ChangePropertyAction TargetObject="{Binding ElementName=imageTitle}" PropertyName="Visibility" Value="Visible"/>
</ic:EventTriggerBehavior>
</i:Interaction.Behaviors>
</Image>
<StackPanel Name="imageTitle" Visibility="Collapsed" Background="Blue"
HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Foreground="White" Text="Dumy Image Title" FontSize="10"/>
</StackPanel>
</Grid>
</DataTemplate>
It's just another way to change a property - JustinXL's answer will provide nice animation with opacity, which will look much better.
I dunno why you specifically want to handle the scenario using XAML. For the Microsoft's recommended MVVM model you should bind a property to your element field and then you can write a converter for the same to return back "Visible" or "Collapse".

ScrollViewer on ItemsControl is always disabled

This is my UserControl file:
<StackPanel>
<StackPanel HorizontalAlignment="Left" Margin="80,0,0,0">
<Grid Width="1110">
...
</Grid>
</StackPanel>
<ScrollViewer VerticalScrollBarVisibility="Visible" Margin="0 0 90 0">
<ItemsControl MinHeight="400" BorderThickness="0" ItemsSource="{Binding MyObjects}" ItemTemplateSelector="{StaticResource myObjectItemsTemplateSelector}" />
</ScrollViewer>
</StackPanel>
Even though the ItemsControl element has a lot of items, the scrollbar is diabled. Why? What am I doing wrong?
The scrollviewer is inside of a stack panel which will size to as much room as the it's child elements need. You can either set a max height on the scrollviewer or switch the parent container to a grid with verticalalignment of stretch which will size to as much room as available.

Scrolling issue in LongListSelector with items of variable height

I have a LongListSelector where each item can contain variable number of images and hence can be of different height. Here's my XAML:
<phone:LongListSelector x:Name="Views" ItemsSource="{Binding}">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding Imgs}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Background="#44AAAAAA" Margin="10,0,10,10">
<Image Source="{Binding photo.Source}" Stretch="UniformToFill"
Height="{Binding Converter={StaticResource ScaleHeight}, Path=photo}" />
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
The problem is while scrolling such LongListSelector when I come across a long item, the scroll position suddenly jumps a few items forward/backward (depending of scroll direction).
I suspect this has something to do with virtualization but I don't know how this can be fixed. Any suggestions?
You should use the Grouped version of the LongListSelector with an empty header, like this you will not have items with such different Height.