How to make Datatemplate derive size from each item separately? - xaml

I want to fill GridView with images. I have Datatemplate for sizing images to fit the GridView. But there is a problem with this template. Image itself is surrounded with Grid within the template. When app runs, this Grid has width derived from first item in the GridView. So when images have different aspect ratio, all following items has template with same sizes as first item. Do I overlook something simple or I need to create something like trigger?
Data template:
<DataTemplate x:Key="DataTemplatePhoto" >
<Grid Margin="0" ScrollViewer.VerticalScrollBarVisibility="Disabled" Height="{Binding Mode=OneWay, Source={StaticResource PhotoListHeight}}" HorizontalAlignment="Left">
<Image Source="{Binding FullURL}" Stretch="Uniform" ScrollViewer.VerticalScrollBarVisibility="Disabled" Margin="0"/>
</Grid>
</DataTemplate>

I think that you need TemplateSelector. Check out this so topic for samples.

Related

Apply Parallax effect into GridView

https://learn.microsoft.com/en-us/windows/uwp/design/motion/parallax
this is microsoft instruction to use the paralax they use the ListView to demo the effect
but my data is stored inside the Grid.View so can anyone show me how to implement into it ?
Microsoft said that we can use this with any element that contain the scrollviewer
ParallaxView works with GridView. The Source property of ParallaxView is for setting the element that either is or contains the ScrollViewer that controls the parallax operation. Here in your code snippet it bind the ForegroundElement to Source property but no such an element in. You need to name the GridView as ForegroundElement .
<GridView
x:Name="ForegroundElement"
IsItemClickEnabled="True"
ItemClick="Grid_Clicked"
ItemsSource="{x:Bind Icons}">
...
</GridView>
Besides this, the code snippet should work well. If you still cannot see the effects it may be caused by items count is not enough. ScrollViewer inside the GridView or ListView only be shown when the host control's layout space is being constrained smaller than the expanded content size, details please see ScrollViewer in a control template. You could try to add more items to check the effects.
Additionally, edit the question to add more details and avoid to put it as an answer.
<GridView Grid.Row="1" ItemsSource="{x:Bind Icons}" IsItemClickEnabled="True" ItemClick="Grid_Clicked">
<GridView.ItemTemplate>
<DataTemplate x:DataType="data:Icon">
<StackPanel>
<Image Width="200" Height="200" HorizontalAlignment="Center" Source="{x:Bind ImageCover}"/>
<TextBlock FontSize="16" VerticalAlignment="Center" Text="{x:Bind Title}"/>
<TextBlock FontSize="10" VerticalAlignment="Center" Text="{x:Bind Room}"/>
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
this is where the data stored
and this is the parallax
<ParallaxView Source="{x:Bind ForegroundElement}" VerticalShift="50">
<!-- Background element -->
<Image x:Name="BackgroundImage" Source="Assets/turntable.png"
Stretch="UniformToFill"/>
</ParallaxView>

How to change listpicker dropdown ItemTemplate background?

I have a Windows Phone 8 listpicker that I'm trying to change the background color of, however, it only seems to change the item background and not the dropdown box background as you can see in the screen capture. The dropdown box seems to be binded to the WP8 theme. How can I change the background of the entire dropdown box and not just each individual item?
<toolkit:ListPicker x:Name="BackgroundListPicker" Background="Black" ItemsSource="{Binding BackgroundsList}">
<toolkit:ListPicker.ItemTemplate >
<DataTemplate x:Name="BackgroundItemTemplate" >
<Grid HorizontalAlignment="Stretch" Background="Black" VerticalAlignment="Stretch" Margin="0,0,0,0">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding BackgroundThumb}" Width="30" Height="30" HorizontalAlignment="Left" />
<TextBlock Text="{Binding BackgroundName}" Foreground="White" Margin="12,0,0,0" HorizontalAlignment="Right"/>
</StackPanel>
</Grid>
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
</toolkit:ListPicker>
So your issue was that you were attempting to apply an image to an item. When you needed to hit the control itself that the items populate via ItemsPresenter so by pulling out the default style template for the control, and either making a place to pass in your image for an instance, or placing one directly into the template itself. You get your resulting background image for the ListPicker background to fall behind the items populating it.
Glad you found your remedy! Cheers.

Screen does not display full content of string

i'm using the textblock to display the content,but for the long content, it just cut off and not display the content fully while i'm sure that the i filled the content string. Pls show me where my code is wrong. Thanks
Link of the my screen: www.flickr.com/photos/37903269#N05/15332152972/
my xaml code :
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<!-- <phone:WebBrowser VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Name="webBrowser1" /> -->
<ListBox Name="Listbox_DetailPage">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Content}"
TextWrapping="Wrap"
Style="{StaticResource PhoneTextNormalStyle}"
HorizontalAlignment="Center"
/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Grid>
I executed the code shared and it seems to wrap text as shown in the screen shot below.
The screenshot you shared seems to have vertical cropping of the text as well. For that, we can set the ScrollViewer.VerticalScrollBarVisibility to 'Auto' with proper Height given.
Also, for your additional knowledge or may be future use, here are some stackoverflow questions which explains the text 'NOT WRAPPING' issue for StackPanel
TextBlock TextWrapping not wrapping inside StackPanel
TextBlock TextWrapping not wrapping
actually, i fixed it.Because of the limitation of sing UI: 4096px limit of size. So there is a need to split the long content in the more than one TextBlock or you can create a scrollabe textbock as here

How to create DataTemplate for several Image objects? (XAML)

I try to display some Images that are stored in ObservableCollection to the xaml page. It's easy to bind xaml object with one image:
<Image Source="{Binding Image}" Stretch="UniformToFill"/>
But how to display a set of images, if I don't know how much images there are? I think that I need to create a DataTemplate, but how it works?
Sorry, I'm not familiar with XAML.
You need to choose one of several controls that commonly used to display multiple items, such as LongListSelector, ItemsControl, etc. Bind the control's ItemsSource to your ObservableCollection of image property. Then define ItemTemplate to tell the control how each item in the ItemsSource should be displayed. For example :
<ItemsControl ItemsSource="{Binding ImageCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image Source="{Binding}" Stretch="UniformToFill"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

Changing width and height of Image inside a datatemplate

I have an image inside datatemplate. I want to change its height and width depending on certain conditions. How can I achieve that?
The XAML code I have:
<ListBox x:Name="options_stack" HorizontalAlignment="Left" Margin="198,569,0,33" Width="603" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollMode="Auto" Height="123" Style="{StaticResource ListBoxStyle}" ItemContainerStyle="{StaticResource ListBoxItemStyle}" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Image x:Name="options_image" Source ="{Binding}" Stretch="Fill" Width="123" Height="123" MaxHeight="123" MaxWidth="123" Tapped="apply_accessory"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Option 1
Since you are binding source property I would create a data structure (to use as DataContext) to hold Source, Width and Height properties and then bind them:
<Image x:Name="options_image"
Source ="{Binding Source}"
Width="{Binding Width}" Height="{Binding Height}"
MaxWidth="{Binding Width}" MaxHeight="{Binding Height}"
Stretch="Fill" Tapped="apply_accessory"/>
Option 2
Another option is to create different DataTemplates and a DataTemplateSelector to apply when certain conditions are met.
DataTemplateSelector ref: http://msdn.microsoft.com/en-us/library/system.windows.controls.datatemplateselector.aspx
Option 3
If you don't like both above options you have another way of doing it. In my last work (Taskin, if you want to take a look you can see the link in my profile) I needed to show different colors for different ListViewItems. I didn't want to create a new Property in my Task model to hold the color or a TemplateSelector just for this. So I created a simple IValueConverter instead that uses an already existent Priority object property and returns its color. Then I binded it like this:
<Border Background="{Binding Priority, Converter={StaticResource priorityToColorConverter}}"/>
XAML gives you many ways to implement what you want and it is up to you to choose the best and cleanest way to solve the problem you are facing.