Referencing an item in a VirtualizingStackPanel - windows-8

I'm playing with the "Grid Application," which is a C++ Metro app template provided by VS11. The main display is a collection of items displayed in a VirtualizingStackPanel:
<ScrollViewer x:name="itemGridScrollViewer">
<GridView x:name="itemGridView">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>
</ScrollViewer>
The actual items are displayed using the Standard250x250ItemTemplate data template defined in StandardStyles.xaml. One of the display elements of this is a graphic.
I'm trying to understand how to reference a specific item so that I can change the graphic at runtime. The ScrollViewer and the GridView have objects associated with them, so I can get to those, but I don't see how to get from there to the individual items.
All this stuff is completely new to me, so be gentle. :)

In general it's not a good idea to dig down into individual templated items in an ItemsControl because not only is it a pain, but that ItemTemplate can regenerate at different times (paging during virtualization, source collection updates, etc) and you'll be left holding an outdated UIElement that's no longer being displayed.
A better approach is to pull the data you need into the item data and in the ItemTemplate use Data Binding to make whatever changes you need to differentiate the UI. One option is to use an IValueConverter to get a different image depending on some state in the item data:
<GridView>
<GridView.ItemTemplate>
<DataTemplate>
<Image Source="{Binding Path=DataState, Converter={StaticResource MyStateToImageConverter}}"/>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
Here I'm assuming that the converter code has been written and an instance of the converter has already been declared as a resource (see here). You could alternatively do a direct Binding to a property on each item's data that specifies its image and skip the converter part.

Related

ItemsControl StatusChanged equivalent in UWP

We were able to understand if ItemsControl finished it's rendering by checking Status within StatusChanged event in WPF.
How can I make sure that ItemsControl finished rendering in UWP? I want to make sure that rendering is completed and access some elements using ContainerFromItem.
I have custom dragging logic using events like ManipulationDelta. After rendering, I want to get the ContentPresenter's position based on it's parent UIElement and then use that position to draw some stuff using Win2D. I need item's position, so I need the container, so I need to make sure containers are rendered in the first place, and it goes like that.
To use ItemsControl in UWP, you also need to know about the UI virtualization. The ItemsControl uses UI virtualization. It means that not all items will be rendered at the same time, instead only the viewable area.
You could try the following code:
<ItemsControl x:Name="item">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Loaded="Grid_Loaded">
<TextBlock Text="{Binding}"></TextBlock>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
I registered the Loaded event for the 'Grid' which is included in every item.

Binding in ListView in XAML Xamarin

Could someone answer me the question about listview?
I have variable MyDatas which is collection of listview (and it works):
<ListView
x:Name="lstView"
HasUnevenRows="True"
ItemsSource="{Binding MyDatas}"> ...
Next I have (part of my code but is enough for explaining the problem):
<Label Grid.Row="0"
Grid.Column="1"
Text="{Binding E1}"
FontSize="{Binding FProp.Size1}"
FontAttributes="{Binding FProp.Atrib1}"/>
E1 is a field of MyDatas and this binding works correct. I have property of fonts in another variable FProp (FProp.Size1 and FProp.Atrib1) and this is problem! How can I use this another variable in this listview?
If FProp is used outside of listview all is ok, if I use it in listview it does not work.
Thank you for help
I believe you are not binding correctly the font size.
You are referencing a property named Size1 inside the object FProp which is not in your current binding context. The main problem is that you can only have one binding context per object. So, you should include the Size1 and Atrib1in your MyDatas object in order to make it available for the label. Another option is to change the binding context of the listview to point to FProp then you could access the values on the labels, but you will lose access to the E1 value. So you will need to choose how to fix this problem depending on the options provided.
You could add an x:Name to your ListView, let's say listView1 and use something like this
{Binding Path=BindingContext.FProp.Size1, Source={x:Reference listView1}}

ISupportIncrementalLoading combined with VariableSizedWrapGrid

I have an ObservableCollection<T> which implements the ISupportIncrementalLoading interface.
When I bind this collection to a normal gridview, everything works fine.
But when I change the ItemsPanel template, to VariableSizedWrapGrid. The incremental loading doesn't work any more.
Xaml that works:
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
Xaml that doesn't work:
<ItemsPanelTemplate>
<VariableSizedWrapGrid Orientation="Vertical" ItemHeight="250" ItemWidth="250" Margin="0,0,80,0"/>
</ItemsPanelTemplate>
I've also found this & this. Stating the VariableSizedWrapGrid doesn't support ISupportIncremetalLoading.
Is there anyone that have written a VariableSizedWrapGrid, that supports Incremental loading? Or an opensource solution?
There are only two panels that support the incremental loading: VirtualizingStackPanel and WrapGrid. I do not know about any open-source solution.
It does not support because the panel itself has to be able to virtualize elements. Since the datatemplates insice a VariableSizedWrapGrid are different, is not possible to virtualize, so every element you want to draw has to be present at the begining.

How to manipulate items in C#

I'm making a Windows Store App using C# and XAML. How can I manipulate one single item in a GridView or a ListView?
According to certain logical conditions, I need to load or not the item in these containers. I have already tried using the property Visibility.Collapsed and Visibility.Visible of the item, but this just hides or shows the item and it is still loaded in the GridView/ListView, and is selectable.
Thanks for your help.
I reckon you meant to hide your GridViewItem without keeping space in view.
You just need to replace GridView's ItemsPanel with StackPanel or VirtualizingStackPanel.
<GridView>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>

Hide list item in Silverlight 4

I have a silverlight application in which I display items in a list box.
I want to hide some items based on a condition, like some value of a string.
My xaml looks like this:
<ListBox
ItemsSource="{Binding DashboardTypes}"
SelectedItem="{Binding SelectedDashboardCategory,Mode=TwoWay}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
Style="{StaticResource ListBoxStyleAttribute}"
Margin="2"
ItemContainerStyle="{StaticResource ListBoxItemStyle}" />
You'll need to set up an ItemTemplate to describe your items and then add a binding on that for the Visibility property that controls when items are visible or not. I suspect that you'll need a converter to actually implement the rule for when an item should be visible.
There is a Stack Overflow question that covers something similar but in WPF. It should give you some pointers that will help you in implementing this.