Items being reordered in LongListSelector - windows-phone-8

In my WP8 app, I'm using LongListSelector to display data items. Grouping works, jump list works.
I have the usual master/detail scenario here - click on item in the list, new page shows up with more information.
The problem is with navigating back to the page with LongListSelector. The list is basically messed up - items are randomly reordered, even between groups. Clicking on an item works properly - ShowItemInfo receives view model for the item the user clicked on.
Previously I was using ListBox, which too suffered from this issue. But I could disable virtualization by using default StackPanel as items panel. I don't know how to disable virtualization on LongListSelector (I don't want to, but the bug is horrible).
View model is simple. I'm using Caliburn.Micro, its conventions and BindableCollection for list of groups. I populate the list just once with AddRange method.
When the page is navigated back to, I'm not doing anything - and I even can't since I use MVVM and Caliburn.Micro. The items are loaded into list in OnInitialize callback, which is invoked only once during lifetime of view model.
The XAML for view is this:
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.Resources>
<phone:JumpListItemBackgroundConverter x:Key="BackgroundConverter"/>
<phone:JumpListItemForegroundConverter x:Key="ForegroundConverter"/>
<Style x:Key="ListJumpListStyle" TargetType="phone:LongListSelector">
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<Border Background="{Binding Converter={StaticResource BackgroundConverter}}"
HorizontalAlignment="Stretch">
<TextBlock Text="{Binding GroupTitle}"
Foreground="{Binding Converter={StaticResource ForegroundConverter}}" />
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<phone:LongListSelector x:Name="Items" LayoutMode="List" IsGroupingEnabled="True"
JumpListStyle="{StaticResource ListJumpListStyle}">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<Grid cal:Bind.Model="{Binding}"
cal:Message.Attach="[Event Tap] = [ShowItemInfo($dataContext)]"
Background="Transparent">
<TextBlock x:Name="ItemText" Style="{StaticResource PhoneTextTitle2Style}" />
</Grid>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
<phone:LongListSelector.GroupHeaderTemplate>
<DataTemplate>
<Border>
<TextBlock Text="{Binding GroupTitle}" />
</Border>
</DataTemplate>
</phone:LongListSelector.GroupHeaderTemplate>
</phone:LongListSelector>
</Grid>

I've narrowed down the issue to the data template, more specifically the attached property Bind.Model, which allows Caliburn.Micro to bind view model to view:
<DataTemplate>
<Grid cal:Bind.Model="{Binding}"
cal:Message.Attach="[Event Tap] = [ShowItemInfo($dataContext)]"
Background="Transparent">
<TextBlock x:Name="ItemText" Style="{StaticResource PhoneTextTitle2Style}" />
</Grid>
</DataTemplate>
When this view is changed to use explicit bindings instead conventions (and Bind.Model attached property is removed), LongListSelector works like a charm.
I believe there has to be a bug within Caliburn.Micro, which causes this issue. Initially, I thought the bug was in the 1.5.2 version, which I'm using (unfortunately I have to), and that it was fixed in new 2.x versions. However, that is not true, the bug is still manifesting in latest stable version 2.0.2.
I was using this technique in WPF without any problems. I've also tried running the app in emulators for WP8.0 and WP8.1, but the behavior is the same.
If anybody finds out, what causes this bug (or new version of Caliburn.Micro fixes it), feel free to edit answer or post a comment. I would be happy to use conventions in data templates again.
My mistake: for data templates, Bind.ModelWithoutContext should be used. Even documentation explicitly says so:
Bind.ModelWithoutContext - View-First - Set’s the Action.Target to the specified instance. Applies conventions to the view. (Use inside
of DataTemplate.)
With Bind.ModelWithoutContext LongListSelector works properly as it should (with conventions in item template).

Related

Create Tab Items and Tab Content dynamically using Orchestra/Catel

I am attempting to create a tabbed interface using Orchestra/Catel. I load the tabs definitions into a TabInfo class. Those work fine - a tabbed interface is created with the correct tab descriptions. In the content for each tab, I want to create a list of buttons - again loaded into a ButtonInfo class. When a tab is selected, the SelectedTab property is used to select the correct list of buttons (ShowButtons).
I have traced the program and when I click on a tab, the correct collection of buttons is in ShowButtons, but nothing shows up in the tab content. I did get this to work in a normal MVVM program, but without the tabs. I used a listview to show my tabs and an ItemsControl to show the buttons.
Belows is my XAML code for the tabs and my SelectedTab logic for pulling the buttons.```
<Grid>
<orccontrols:TabControl LoadTabItems="LazyLoading" ItemsSource="{Binding TabInfo}" SelectedItem="{Binding SelectedTab}">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding TabDesc}"/>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding ShowButtons}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical" IsItemsHost="True"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding ButtonDesc}"
MinWidth="150"
Height="30"
FontSize="12"
FontWeight="Bold"
Margin="0,15,25,10"
Padding="5,1">
<Button.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding OracleJob}" Value="0">
<Setter Property="Button.Background" Value="DarkSalmon" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</TabControl.ContentTemplate>
</orccontrols:TabControl>
</Grid>
And the code for populating ShowButtons:
public void OnSelectedTabChanged()
{
int _selectTab = SelectedTab.TabKey;
var _showButtons = ButtonInfo.Where(i => i.ButtonTab == _selectTab);
ObservableCollection<ButtonRecord> _btn = new ObservableCollection<ButtonRecord>(_showButtons);
ShowButtons = _btn;
}
Any help is greatly appreciated. I am a beginner with Orchestra/Catel, so I am probably missing the best way to do this.
I recommend to split up the issue from Orchestra / Catel. Orchestra allows you to provide a view as shell, but this view can live on it's own (the only thing Orchestra does is place it inside the correct location inside the shell). By making this problem unrelated to Orchestra, it makes it a bit easier for us to solve.
Next up, I recommend looking into the Catel docs. There is a full example on how to show a shell with tabs with closable tab buttons, see https://docs.catelproject.com/vnext/tips-tricks/mvvm/using-tabbed-interface-with-mvvm/

Binding pivotitem content in WP8.1

In my mainpage.xaml I have a pivot control:
<Pivot x:Name="Pivot"
ItemsSource="{Binding PivotItems}"
ItemTemplate="{StaticResource PivotItemTemplate}">
</Pivot>
which binds it's items to a PivotsItems property on my viewmodel. The pivotitems are shown correctly but i'm not able to bind the content of the pivotitem.
My datatemplate looks like:
<DataTemplate x:Key="PivotItemTemplate">
<PivotItem>
<TextBlock Text="Test"></TextBlock>
</PivotItem>
</DataTemplate>
But no content is shown in any pivotitem.
What I wan't to do is to bind the pivotitems (which works) but show a seperate filter for each pivotitem. In this example I just want to show a TextBlock in each pivotitem.
SOLVED:
I defined the itemtemplate on the pivot control in the page, while i had to add it when populating the list in the vm:
PivotItems.Add(new PivotItem { Header = item.Key, ContentTemplate = App.Current.Resources["PivotItemTemplate"] as DataTemplate });
Pivot automatically Wraps it's items into PivotItems (like you don't add ListViewItem to a ListViews ItemsTemplate). Just put your content into the DataTemplate.
<DataTemplate x:Key="PivotItemTemplate">
<TextBlock Text="Test"></TextBlock>
</DataTemplate>
The header defaults to the DataContext of the PivotItem (so your list item). If you want it to display a certain property, you have to set the HeaderTemplate too:
<DataTemplate>
<TextBlock Text="{Binding MyProperty}" />
</DataTemplate>
If you just keep it as a TextBlock with no further properties, the default style gets applied perfectly.

Panorama-control don't remember selected panorama item when navigating

I'm using a panorama control in my wp8 app that consists of 4 panorama-items. These items are all i separate files. The content within the items are bound to viewmodels, (using MVVM Cross).
The problem is when I navigate from a panorama-item to a new page and go back, the panorama control shows the first panorama-item (it kinda resets).
I've tried to override OnNavigatedTousing the selectedIndexon the panorama control, but it only returns -1.
The xaml class containing the panorama-control:
<Grid x:Name="LayoutRoot" Background="Transparent">
<!--Panorama Control-->
<!--Cannot bind directly to background with ImageBrush, using converter instead-->
<controls:Panorama x:Name="panorama"
Background="{Binding Home.WindowsBackgroundUrl, Converter={StaticResource ImageBrushConverter}}">
<controls:Panorama.Title>
<StackPanel Orientation="Vertical" Margin="0,60,0,0">
<Image x:Name="Icon" Source="/Images/PanoramaLogo.png" />
</StackPanel>
</controls:Panorama.Title>
<!--Panorama items-->
<ScrollViewer>
<local:InterestView x:Name="Interest" Margin="0,-20,0,0" />
</ScrollViewer>
<ScrollViewer>
<local:CustomerCategoryPanoramaItem x:Name="Customer" Margin="0,-20,0,0"/>
</ScrollViewer>
<ScrollViewer>
<local:MapListView x:Name="MapList" Margin="0,-20,0,0"/>
</ScrollViewer>
<ScrollViewer>
<local:ContactMeView Margin="0,-20,0,0"/>
</ScrollViewer>
</controls:Panorama>
</Grid>
Thanks for any help
If you are persisting your screen in view stack than it panorama-control will take care of that other-vice we have to persist selected panorama while we are navigating forward and when we come back to Panorama control. For that we has to set its DefaultItem property .
Panorama numbers its view starting from 0, Like for if we have three panorama Items than it will give 0 to first, 1 to second and 2 to third.
Example :
PanoramaControl.DefaultItem = PanoramaControl.Items[0 to 2];

ListViewItems become blank when enabled NavigationCache and IncrementalUpdateBehavior

Here is my code:
<ListView ItemsSource="{Binding Items}" IsItemClickEnabled="True" ItemClick="ItemView_ItemClick" ContinuumNavigationTransitionInfo.ExitElementContainer="True">
<ListView.ItemTemplate>
<DataTemplate>
<Border BorderBrush="{StaticResource ButtonBorderThemeBrush}" BorderThickness="0,0,0,3">
<StackPanel Margin="0,0,0,9.5">
<TextBlock
Text="{Binding Title}"
TextWrapping="Wrap"
Pivot.SlideInAnimationGroup="1"
CommonNavigationTransitionInfo.IsStaggerElement="True"
Style="{ThemeResource ListViewItemTextBlockStyle}"
Margin="0,0,19,0">
<interactivity:Interaction.Behaviors>
<core:IncrementalUpdateBehavior Phase="1"/>
</interactivity:Interaction.Behaviors>
</TextBlock>
<TextBlock
Text="{Binding Description}"
TextWrapping="WrapWholeWords"
Pivot.SlideInAnimationGroup="2"
CommonNavigationTransitionInfo.IsStaggerElement="True"
Style="{ThemeResource ListViewItemContentTextBlockStyle}"
Margin="0,0,19,0">
<interactivity:Interaction.Behaviors>
<core:IncrementalUpdateBehavior Phase="2"/>
</interactivity:Interaction.Behaviors>
</TextBlock>
</StackPanel>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Then, I set NavigationCacheMode = NavigationCacheMode.Required in code-behind. When I navigate to ItemPage and go back. ListViewItems will become blank when you slide it and never show again. They just disappear like this:
Finally, I find out the problem. It is a bug.
Because in the IncrementalUpdateBehavior, it use a function called "FindContentTemplateRoot" to search the visual tree for associated object's ContentTemplateRoot. When I navigate to another page, it raise associated object's Unloaded event and search the visual tree again. But this time it failed because it parent had been unloaded. VisualTreeHelper.GetParent() returns null.
So, it cause a bug that this Behavior can't normally uncache the associated object. When page goes back, associated object caches again. So that the associated object's opacity and DataContext will set twice or more. It just messed up.

How to edit ExpandableContentTemplate in Blend

I'm using Telerik RadExpanderControl in my Wp8 app. I open the project in Blend, right-click the RadExpanderControl under Objects and Timeline, select Edit Additional Templates. Here my options are Edit Generated Content (ContentTemplate), Edit AnimatedIndicatorContentTemplate and Edit ExpandedStateContentTemplate.
None of these actually provide me access to the ExpandableContentTemplate which is what I need.
Am I missing something?
Unfortunately, is isn't available as an extractable template. However you can do this to create one:
<telerikPrimitives:RadExpanderControl.ExpandableContentTemplate>
<DataTemplate>
<StackPanel Margin="24, 4, 4, 4" Orientation="Horizontal">
<TextBlock
FontSize="{StaticResource PhoneFontSizeExtraLarge}"
FontFamily="{StaticResource PhoneFontFamilyLight}"
Text="{Binding TextYouWantToBind}"
VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</telerikPrimitives:RadExpanderControl.ExpandableContentTemplate>
A workaround that I do when I need to use Blend to style the ExpandableContentTemplate is to style the DataTemplate of the (Generated)ContentTemplate to my liking, then copy paste it into the DataTemplate of the ExpandablecontentTemplate.
I have reported this to the windows Phone Team