List Item template for dynamic number of bindings - windows-phone-7

Hei,
So lets assume that I have this ListBox.ItemTemplate:
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="DataTemplate1">
<StackPanel Orientation="Horizontal">
<StackPanel Height="100" Width="100">
<TextBlock TextWrapping="Wrap" Text="{Binding name}" FontWeight="Bold"/>
<TextBlock TextWrapping="Wrap" Text="{Binding age}" FontWeight="Bold"/>
</StackPanel>
<StackPanel Height="100" Width="100">
<Image Height="100"/>
<Image Height="100"/>
</StackPanel>
<StackPanel Height="100" Width="100">
<TextBlock TextWrapping="Wrap" Text="blah blah" FontSize="13.333"/>
<TextBlock TextWrapping="Wrap" Text="{Binding something}"/>
</StackPanel>
<StackPanel Height="100" Width="100">
<TextBlock TextWrapping="Wrap" Text="Time" FontSize="13.333"/>
<TextBlock TextWrapping="Wrap" Text="45 minutes"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
What I want to achieve is that somehow the number of Images in the second stackpanel, this one:
<StackPanel Height="100" Width="100">
<Image Height="100"/>
<Image Height="100"/>
</StackPanel>
to be dynamic, two for some list box items, 3 or 4 for others.
I am wondering if its possible to achieve this with binding and templates ?
I don't want to do this manually in code.

You can replace that particular StackPanel with a ListBox. The ListBox can be then bind to the Image-collection and its ItemTemplate can be set to show the images. Like this:
<DataTemplate x:Key="DataTemplate1">
<StackPanel Orientation="Horizontal">
<StackPanel Height="100" Width="100">
<TextBlock TextWrapping="Wrap" Text="{Binding name}" FontWeight="Bold"/>
<TextBlock TextWrapping="Wrap" Text="{Binding age}" FontWeight="Bold"/>
</StackPanel>
<ListBox ItemsSource="{Binding DynamicCollectionOfImages}">
<ListBox.ItemTemplate>
<DataTemplate>
<Image Height="100" Source="{Binding .}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<StackPanel Height="100" Width="100">
<Image Height="100"/>
<Image Height="100"/>
</StackPanel>
<StackPanel Height="100" Width="100">
<TextBlock TextWrapping="Wrap" Text="blah blah" FontSize="13.333"/>
<TextBlock TextWrapping="Wrap" Text="{Binding something}"/>
</StackPanel>
<StackPanel Height="100" Width="100">
<TextBlock TextWrapping="Wrap" Text="Time" FontSize="13.333"/>
<TextBlock TextWrapping="Wrap" Text="45 minutes"/>
</StackPanel>
</StackPanel>
</DataTemplate>

When there are different set of images for every list item you should add another ListBox inside main one and put an Image control as an ItemTemplate.
But when you have just some images-sets (eg. with 2, 3 and 4 static images) for whole list and want to display one of them for every list item you can prepare 3 StackPanel's inside listBoxItem template and change it visibility depending on some property from DataSource. This property value should be converter into a Visibility enumeration member.
Eg. when that images should depend on integer ImagesSet property from DataSource:
<StackPanel Height="100" Width="100" Visibility={Binding ImagesSet, Converter={StaticResources ImagesSetToVisibility}, ConverterParameter=2}>
<Image Height="100"/>
<Image Height="100"/>
</StackPanel>
<StackPanel Height="100" Width="100" Visibility={Binding ImagesSet, Converter={StaticResources ImagesSetToVisibility}, ConverterParameter=3}>
<Image Height="100"/>
<Image Height="100"/>
<Image Height="100"/>
</StackPanel>
<StackPanel Height="100" Width="100" Visibility={Binding ImagesSet, Converter={StaticResources ImagesSetToVisibility}, ConverterParameter=4}>
<Image Height="100"/>
<Image Height="100"/>
<Image Height="100"/>
<Image Height="100"/>
</StackPanel>
The converter should check, if value is equal to parameter and return Visibility.Visbile or Visibility.Collapsed:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((int)value) == ((int)parameter) ? Visibility.Visible : Visibility.Collapsed;
}

Related

How to wrap ListPicker SelectedItem in windows phone 8

I am using ListPicker from windows phone toolkit. My options are pretty lengthy so my ItemTemplate is,
<DataTemplate>
<TextBlock TextWrapping="Wrap"/>
</DataTemplate>
Now, when I select a lengthy option(say three lines) in Expansion Mode (less then 5 options) it is working good as expected, but when I Use FullMode(more than 5 items) it is not wrapped in the ListPicker.
To be more clear, It is wrapped as expected in the FullModePage but it is not within the control.
<toolkit:ListPicker Margin="0" MinHeight="70" toolkit:TiltEffect.SuppressTilt="True" Template="{StaticResource ListPickerControlCustomTemplate}" HorizontalAlignment="Stretch" VerticalAlignment="Top" FontFamily="{StaticResource RegularFont}"
SelectionChanged="SharedAppsCategoryListPicker_OnSelectionChanged" Name="SharedAppsCategoryListPicker"
LayoutUpdated="SharedAppsCategoryListPicker_OnLayoutUpdated" >
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<Grid Margin="0,0,20,0">
<TextBlock TextWrapping="Wrap" VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="20,15,120,15" FontSize="20" Text="{Binding groupname}" Foreground="{StaticResource ListPickerForegroundColor}"></TextBlock>
<Rectangle Margin="20,10,0,-11" Height="0.5" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Stroke="{StaticResource ListPickerDividerColor}" StrokeThickness="0.5" />
</Grid>
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<Grid Margin="0,0,20,0">
<TextBlock MaxWidth="250" Width="250" TextWrapping="Wrap" FontSize="20" Margin="0,0,120,0" VerticalAlignment="Center" Text="{Binding groupname}" Foreground="{StaticResource ListPickerForegroundColor}" FontFamily="{StaticResource RegularFont}"></TextBlock>
</Grid>
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>
you would be needed to give width for the Textblock after then Text Would be Wrapped if content is larger than the width of the Textblock.
try this :
<DataTemplate>
<TextBlock TextWrapping="Wrap" width=200/>
</DataTemplate>
Edit:
I have Implemeted it this way
XAML:
<toolkit:ListPicker Margin="0" MinHeight="70" toolkit:TiltEffect.SuppressTilt="True" HorizontalAlignment="Stretch" VerticalAlignment="Top" Name="SharedAppsCategoryListPicker" >
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<Grid Margin="0,0,20,0">
<TextBlock Width="300" TextWrapping="Wrap" VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="20,15,120,15" FontSize="20" Text="{Binding}" ></TextBlock>
</Grid>
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<Grid Margin="0,0,20,0">
<TextBlock Width="400" TextWrapping="Wrap" FontSize="20" Margin="0,0,0,0" VerticalAlignment="Center" Text="{Binding}" ></TextBlock>
</Grid>
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>
Images of O/P:

Changing the image of the selected item in listbox (Windows Phone)

I have this code:
<ListBox x:Name="lisbox1" Margin="-24,0,-9,0" CacheMode="BitmapCache" ItemsSource="{Binding Teacher}" HorizontalAlignment="Right" MouseLeftButtonUp="lisbox1_MouseLeftButtonUp" SelectionChanged="lisbox1_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="0,0,0,-35">
<Image x:Name="image1" Stretch="Fill" Source="ticket.png" Margin="-15,-16,3,21"/>
<TextBlock x:Name="asda" Margin="56,50,0,0" TextWrapping="Wrap" TextAlignment="Left" Text="{Binding lastname}" Style="{StaticResource PhoneTextLargeStyle}" Height="50" VerticalAlignment="Top" HorizontalAlignment="Left" Width="264" />
<TextBlock Margin="56,106,43,90" TextWrapping="Wrap" TextAlignment="Left" Text="{Binding firstname}" Style="{StaticResource PhoneTextLargeStyle}" />
<TextBlock Margin="0,50,43,147" Text="{Binding auditnumb}" Style="{StaticResource PhoneTextLargeStyle}" RenderTransformOrigin="0.515,-1.286" HorizontalAlignment="Right" Width="98" />
<Border Background="Silver" BorderThickness="1,1,1,1" Height="40" CornerRadius="20,20,20,20" Margin="56,3,60,0" VerticalAlignment="Top">
<TextBlock TextWrapping="Wrap" Text="{Binding subname}" TextAlignment="Center" VerticalAlignment="Top" Style="{StaticResource PhoneTextTitle3Style}"/>
</Border>
<Border BorderThickness="1" Height="40" Background="Green" Width="69" CornerRadius="25,25,25,25" Margin="316,154,61,41" Name="border1" >
<TextBlock Text="{Binding End}" TextWrapping="Wrap" TextAlignment="Center" Style="{StaticResource PhoneTextTitle3Style}" Width="69" Margin="-2,1,-3,-1"/>
</Border>
<Border BorderThickness="1" Background="Red" CornerRadius="25,25,25,25" Name="border2" Height="40" Width="69" Margin="230,154,147,41" >
<TextBlock TextAlignment="Center" TextWrapping="Wrap" Text="{Binding Start}" Style="{StaticResource PhoneTextTitle3Style}" Width="69" Margin="-2,1,-3,1"/>
</Border>
<Border Background="WhiteSmoke" CornerRadius="40" BorderThickness="1" Height="40" HorizontalAlignment="Left" Margin="16,88,0,0" Name="border3" VerticalAlignment="Top" Width="40" >
<TextBlock Margin="5" Foreground="Black" TextAlignment="Center" TextWrapping="Wrap" Text="{Binding PairNumber}" Width="28" RenderTransformOrigin="0.304,-0.462" Style="{StaticResource PhoneTextNormalStyle}" HorizontalAlignment="Left" Height="28" VerticalAlignment="Top"/>
</Border>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
How to change the image when you select an item from the listbox? I have tried using VisualStateManager, but nothing happens.
It's hard to tell the best way without more code, but one way to do this is to add a property for the image path to the 'Teacher' class and bind the image source to it. Then when the item is clicked, change the image path in the Teacher object. (make sure your teacher class correctly supports OnPropertyChanged() for the image path property).
Use the following code to change the image source in C# (just put it after the desired event handler)
var image = (Image)sender;
image.Source = new Uri("/AppName;component/Images/imageName.png", UriKind.Relative)
Make sure that the image you want to use is saved to your Image folder
(Replace spaces in your app name with %20)

WP7: how to count children of a list box in datatemplate?

I have created a pivot control to show all data from my database by using binding data. And the code for the pivot control look like this. And the UI view here: photo
I want to solve the problem: After I tap on an item of listbox (of pivot control), I will get the name of the item.
Note: All controls have no name (x:name). I am trying to use Tag="{Binding CHUPHONG}" replace for x:name.
Could you show me the way to solve that? Or Where I am wrong? Thanks so much!
<controls:PivotItem Header="PHÒNG CHƠI" FontSize="19" Margin="0" Background="{x:Null}">
<ListBox x:Name="listDSPhong">
<ListBox.ItemTemplate>
<DataTemplate>
<toolkit:WrapPanel Height="120" Width="480" Margin="0,10,0,20">
<Image HorizontalAlignment="Left" Height="120" VerticalAlignment="Top" Width="120" Source="/LuanVanDeMo;component/xephinhlogo.png"/>
<TextBlock Margin="4,44,-98,44" TextWrapping="Wrap" Text="Chủ phòng:" HorizontalAlignment="Left"/>
<TextBlock Height="32" Margin="0,5,-98,0" TextWrapping="Wrap" Text="Tên phòng:" VerticalAlignment="Top" HorizontalAlignment="Left" Width="98"/>
<TextBlock Margin="0,0,-98,4" TextWrapping="Wrap" Text="Số lượng:" Height="32" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="98"/>
<TextBlock Margin="102,44,-242,44" TextWrapping="Wrap" Text="{Binding TENPHONG}" Width="140" Height="32" HorizontalAlignment="Left"/>
<TextBlock Height="32" Margin="102,5,-242,0" TextWrapping="Wrap" VerticalAlignment="Top" Text="{Binding CHUPHONG}" Width="140" HorizontalAlignment="Left"/>
<TextBlock Margin="102,0,-134,4" TextWrapping="Wrap" Height="32" VerticalAlignment="Bottom" Text="{Binding SOLUONG}" Width="32" HorizontalAlignment="Left"/>
<Image HorizontalAlignment="Left" Width="50" Source="/LuanVanDeMo;component/Images/Buttons/lock.png" Margin="285,35,0,35" Height="50"/>
</toolkit:WrapPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</controls:PivotItem>
</controls:Pivot>
ItemContainerGenerator.ContainerFromIndex(SelectedIndex);
Fetching Selected Item

Hide the template elements in code-behind file

I have a LongListHeaderTemplate that consists of a list and a button. You will see how are they distributed in the code below. My problem is that I need sometimes hide button in code-behind file. How I can do that?!
XAML:
<toolkit:LongListSelector Grid.Row="1" ListHeader="{Binding EpsLaterItems}" IsFlatList="True" ItemsSource="{Binding EpsItems}" x:Name="EPS" SelectionChanged="EPS_SelectionChanged_1">
<toolkit:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ImageUrl, Converter={StaticResource BmpConverter}}" Width="100" Height="100" VerticalAlignment="Top" Margin="0,10,18,0"/>
<StackPanel Margin="0,0,0,20" Width="300">
<TextBlock Text="{Binding Date, ConverterCulture=ru-RU, StringFormat=f}"
FontSize="22" FontWeight="Bold" Foreground="#FFD65B2D" />
<TextBlock Text="{Binding Title}" TextWrapping="Wrap" FontSize="22" Foreground="Aqua" />
</StackPanel>
</StackPanel>
</DataTemplate>
</toolkit:LongListSelector.ItemTemplate>
<toolkit:LongListSelector.ListHeaderTemplate>
<DataTemplate>
<StackPanel>
<ListBox x:Name="listbox1" ItemsSource="{Binding}" ScrollViewer.VerticalScrollBarVisibility="Disabled" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ImageUrl, Converter={StaticResource BmpConverter}}" Width="100" Height="100" VerticalAlignment="Top" Margin="0,10,18,0"/>
<StackPanel Margin="0,0,0,20" Width="300">
<TextBlock Text="{Binding Date, ConverterCulture=ru-RU, StringFormat=f}"
FontSize="22" FontWeight="Bold" Foreground="#FFD65B2D" />
<TextBlock Text="{Binding Title}" TextWrapping="Wrap" FontSize="22" Foreground="Aqua" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button x:Name="footerEpsLater" Content="more..."
FontSize="30" Foreground="SteelBlue" Style="{StaticResource StyleForButton}" BorderBrush="#FFFF2121"/>
</StackPanel>
</DataTemplate>
</toolkit:LongListSelector.ListHeaderTemplate>
<toolkit:LongListSelector.ListFooterTemplate>
<DataTemplate>
<Button Content="more..." FontSize="30" Foreground="SteelBlue" Click="footerEPS_Click_1" Style="{StaticResource StyleForButton}"/>
</DataTemplate>
</toolkit:LongListSelector.ListFooterTemplate>
</toolkit:LongListSelector>
If i do like this footerEpsLater.Visibility = Visibility.Collapsed; I get the error: footerEpsLater does not exist in this context

How Can i set List Box with in A pivot

my problem is how can i set listbox which has itemtemplate ,and it also itemteplate of pivot
.i don't have any idea to doing this but it is a need of my project .one another is how to set pivot header . i m trying to do something like that
<controls:Pivot Height="779" Name="m" >
<controls:Pivot.Background>
<ImageBrush ImageSource="/WindowsPhoneApplication7;component
/Images/S.jpeg" />
</controls:Pivot.Background>
<!--<controls:PivotItem Name="all" >-->
<controls:Pivot.ItemTemplate>
<DataTemplate>
<ListBox Height="450" HorizontalAlignment="Stretch" Margin="8,6,0,0" Name="listBox1" VerticalAlignment="Stretch" Background="#00537393" Width="445" >
<ListBox.ItemTemplate >
<DataTemplate >
<Border BorderBrush="Black" CornerRadius="8" BorderThickness="1" HorizontalAlignment="Stretch" Name="border">
<Grid HorizontalAlignment="Stretch" Name="grid1" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="0*" />
<RowDefinition Height="100*" />
</Grid.RowDefinitions>
<Image Height="78" HorizontalAlignment="Left" Margin="13,12,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="92" Grid.Row="1" Source="{Binding ImageSource}" />
<TextBlock Foreground="White" Height="80" HorizontalAlignment="Left" Margin="111,12,0,0" Name="textBlock1" Text="{Binding Title}" VerticalAlignment="Top" Width="280" TextWrapping="Wrap" Grid.Row="1" />
<Image Grid.Row="1" Height="75" HorizontalAlignment="Left" Margin="380,12,0,0" Name="image2" Stretch="Fill" VerticalAlignment="Top" Width="73" Source="/WindowsPhoneApplication7;component/Images/appbar.transport.play.rest.png" />
<TextBlock Foreground="White" Grid.Row="1" Height="20" HorizontalAlignment="Left" Margin="111,88,0,0" Name="textBlock2" Text="{Binding date}" VerticalAlignment="Top" Width="190" FontSize="11" />
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
</controls:Pivot.ItemTemplate>
<controls:Pivot.HeaderTemplate>
<DataTemplate>
<TextBlock Text="hiii" TextWrapping="Wrap" FontSize="0" Foreground="White" />
</DataTemplate>
</controls:Pivot.HeaderTemplate>
</controls:Pivot>
You can set the header of a Pivot control to pretty much anything since it is an object. To set it to an image instead of a default title set the Title to "" and do this in the constructor of your page:
YourPivotControl.Title = new Image { Width = 400, Source = new BitmapImage(new Uri("/someImage.png", UriKind.Relative)) };
Did you take a look at the default pivot page setup by visual studio? They do exactly what you're looking for:
<!--Pivot Control-->
<controls:Pivot Title="MY APPLICATION">
<!--Pivot item one-->
<controls:PivotItem Header="first">
<!--Double line list with text wrapping-->
<ListBox x:Name="FirstListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432" Height="78">
<TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</controls:PivotItem>

Resources