display a long text inside a textblock on two lines - windows-phone-7

I have a listbox which contains a textblock and an image. I want if the text is too long it goes to a second line. At the moment only have of the text is visible. Here is my XAML
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding imgurl}" MaxHeight="120" MaxWidth="120" Margin="0,10,0,0" />
<TextBlock Text="{Binding title}" Margin="50,0,12,0" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
Could anybody help me?
Screenshot

Ah ya, so I guess it is as simple as it looked. You just need to restrict the width of where your text is and invoke TextWrapping, which you have multiple options to accomplish that. Here's a couple possibilities...
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding imgurl}" MaxHeight="120" MaxWidth="120" Margin="0,10,0,0" />
<TextBlock Text="{Binding title}" Margin="50,0,12,0" TextWrapping="Wrap" MaxWidth="{Binding ActualWidth, ElementName=Self}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
Or you could have a panel do it which should get restricted by its parent...
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="0,10,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Source="{Binding imgurl}" MaxHeight="120" MaxWidth="120" />
<TextBlock Grid.Column="1" Text="{Binding title}" TextWrapping="Wrap" Margin="50,0,2,0" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
Or depending on how this ListBox is layed out as a child, it could possibly even be as easy as just adding TextWrapping="Wrap" to the TextBlock and then setting the MaxWidth on the ListBox itself. Either way, hope this helps... :)

Related

Why Does Blend Fail To Show Text in Generated Item Template?

I am trying to get some design data in blend expressions for my wp7 app but after a certain point it seems to just lose the data and shows me nothing
As you can see I see nothing in the rectangle error even though there is a hardcoded value in it.
<Grid x:Name="LayoutRoot"
Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentGrid"
Grid.Row="1">
<ListBox x:Name="lbMovieCollection" ItemsSource="{Binding MovieCollections}">
<ListBox.Resources>
<DataTemplate x:Key="lbMovieCollectionItemTemplate">
<toolkit:ExpanderView x:Name="expMovieCollection" ItemsSource="{Binding Movies}" Header="{Binding Mode=OneWay}" Width="480">
<toolkit:ExpanderView.Resources>
<DataTemplate x:Key="expMovieCollectionHeaderTemplate">
<TextBlock TextWrapping="Wrap" Text="{Binding Name}" Height="50"/>
</DataTemplate>
<DataTemplate x:Key="expMovieCollectionItemTemplate">
<ListBoxItem HorizontalAlignment="Left" toolkit:TiltEffect.IsTiltEnabled="True" d:LayoutOverrides="VerticalAlignment">
<StackPanel Height="61" Width="371" >
<TextBlock TextWrapping="Wrap" Text="THIS IS HARCDOED" FontSize="29.333" />
</StackPanel>
</ListBoxItem>
</DataTemplate>
</toolkit:ExpanderView.Resources>
<toolkit:ExpanderView.ItemTemplate>
<StaticResource ResourceKey="expMovieCollectionItemTemplate"/>
</toolkit:ExpanderView.ItemTemplate>
<toolkit:ExpanderView.HeaderTemplate>
<StaticResource ResourceKey="expMovieCollectionHeaderTemplate"/>
</toolkit:ExpanderView.HeaderTemplate>
</toolkit:ExpanderView>
</DataTemplate>
</ListBox.Resources>
<ListBox.ItemTemplate>
<StaticResource ResourceKey="lbMovieCollectionItemTemplate"/>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Grid>
Looks like your "THIS IS HARCDOED" values are hidden inside the collapsed portion of toolkit:ExpanderView control.

Changing UI according to text size in wp7

<ListBox Margin="0,2,0,0" SelectionChanged="OnSelectionChanged" ScrollViewer.VerticalScrollBarVisibility="Disabled" x:Name="lstUcpPanel" ItemContainerStyle="{StaticResource myContainerStyle}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel Width="Auto" Height="Auto" Margin="-7,-8,-7,0" ItemWidth="246" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="1" Margin="3.5,1,0,0" BorderBrush="Transparent">
<StackPanel Orientation="Vertical" Background="LightGray">
<Image Height="115" Width="115" Margin="2" Source="{Binding categoryImageName}" ></Image>
<TextBlock Text="{Binding name}" TextWrapping="Wrap" Foreground="Black" HorizontalAlignment="Center"></TextBlock>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I am using the above code for showing a data as in following format:
but when the [name] is long like "Settings flkjadsflka fdsaflksdj flhasdjhfl asd", then the design becomes like this:
What i want to know is that is there anyway i can show the items in a uniform format without any distortion with uniform height of the item.
Set the stretch property on your image to Stretch="Fill"
<Image Height="115" Width="115" Margin="2" Source="{Binding categoryImageName}" Stretch="Fill" ></Image>
How about giving the items in the WrapPanel a fixed Height?
<toolkit:WrapPanel ItemHeight="80" ... />

Use of WrapPanel

I have to wrap a content in text block which is in the stack panel. The following XAML code is
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Width="300">
<Image Height="160" HorizontalAlignment="Left" Margin="0,0,-400,0" VerticalAlignment="Top" Width="175" Source="{Binding thumb}"/>
<!--ContentControl Width="150" Height="110" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0,0,-400,0" Content="{Binding Image}"/>-->
<TextBlock TextWrapping="Wrap" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="190,-167,-200,0" Text="{Binding title}"/>
<TextBlock TextWrapping="Wrap" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="190,-135,-200,0" Text="{Binding page}"/>
<TextBlock FontSize="15" TextWrapping="Wrap" Height="Auto" Margin="190,-95,-200,0" Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate
When i specify the width of the text block the text wrap works in the vertical and horizontal orientation.
I want the text to wrap in the vertical view only and in the horizontal view the text should not wrap without mention the textblock width.
for example in vertical view the list box width is small so the text should be:
match is between India and
pakistan
in horizontal view..I need it in single line
match is between India and pakistan.
Thanks in Advance!
>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Grid.Column="0" ... />
<StackPanel Grid.Column="1">
<TextBlock TextWrapping="NoWrap" ... />
<TextBlock TextWrapping="NoWrap" ... />
<TextBlock TextWrapping="Wrap" ... />
</StackPanel>
</Grid>
</DataTemplate>

Get content of control inside listbox item when it was clicked

I have a ItemTemplate like this
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Button Grid.Row="0">
<Button.Template>
<ControlTemplate>
<Border BorderBrush="AntiqueWhite" BorderThickness="1">
<StackPanel Orientation="Horizontal" Background="Gray" Width="465">
<Image Margin="2,0,10,0" Source="{Binding StateImage}" Stretch="None"/>
<TextBlock Name="txt" Text="{Binding DateLabel}"/>
</StackPanel>
</Border>
</ControlTemplate>
</Button.Template>
<Custom:Interaction.Triggers>
<Custom:EventTrigger EventName="Click">
<mx:EventToCommand Command="{Binding VisibilityListboxCommand}"
CommandParameter="{Binding EementName=txt, Path=Text}"
/>
</Custom:EventTrigger>
</Custom:Interaction.Triggers>
</Button>
<ListBox Grid.Column="1" ItemsSource="{Binding WhatsonList, Mode=OneWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Gray" BorderThickness="0,0,0,1" Padding="0,0,0,10">
<Grid Margin="0,10,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="360"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0">
<Button>
.....
</Button>
<CheckBox Template="{StaticResource CheckboxImageTemplate}" Margin="0,5,0,0"/>
</StackPanel>
<ListBox Grid.Column="1">
.....
</ListBox>
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
When I click on button inside listboxitem datatemplate I want to get content of textblock txt to find out what listbox item was clicked to trace back index in the List(model) that listbox bind from.
But from commandparameter I cannot get anything because there are many textblock named txt I think.
Please help me !
A better way to solve this is by binding the CommandParameter to 'thing' in the DataContext of the item like this:
<Custom:Interaction.Triggers>
<Custom:EventTrigger EventName="Click">
<mx:EventToCommand Command="{Binding VisibilityListboxCommand}"
CommandParameter="{Binding}" />
</Custom:EventTrigger>
</Custom:Interaction.Triggers>
This way the binding does not rely on a particular control being present and having a specific name.

Scrolling in Usercontrol

I have developed one user control, which has ListBox. when we scrolling it is not happen for me can u tell me what would goes wrong in it?
I have following items,
1.Panorama Page
In side controls:PanoramaItem I have created the instance of usecontrol
<controls:PanoramaItem Header ="Header">
<Grid>
<views:MyUserControlView DataContext="{Binding MyViewModel}" />
</Grid>
</controls:PanoramaItem>
2.MyUserControlView
<UserControl x:Class="UI.Views.RecentFileView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:utility="clr-namespace:UI.CommandBehaviours"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="364" d:DesignWidth="245">
<Grid x:Name="LayoutRoot" Height="360">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ListBox x:Name="RecentFilesListBox" Grid.Row="0" ItemsSource="{Binding RecentFiles}"
utility:CommandService.Command="{Binding ToFileViewCommand}" utility:CommandService.CommandParameter="{Binding SelectedItem, ElementName=RecentFilesListBox}" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="DataTemplateStackPanel" Orientation="Horizontal">
<Image x:Name="ThumbnailImage" Source="{Binding Path=Thumbnail}" Height="43" Width="43" VerticalAlignment="Top" Margin="10,0,20,0"/>
<StackPanel>
<TextBlock x:Name="FileNameTextBlock" Text="{Binding Path=FileName, Mode=OneWay}" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock x:Name="FileserverNameTextBlock" Text="{Binding Path=FileServerName}" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
</Grid>
When I am Scrolling Its not working for me..
Assuming you mean vertical scrolling, then the structure you've shown should work OK - although I'm a little bit concerned about where the <ListBox> is in your <UserControl>
Here are two things that should work:
Option 1:
<Panorama>
<PanoramaItem>
<MyUserControl>
</PanoramaItem>
</Panorama>
where MyUserControl is:
<UserControl>
<ScrollViewer>
<StackPanel>
... lots of <TextBlock>s
</StackPanel>
</ScrollViewer>
</UserControl>
or...
Option 2.
<Panorama>
<PanoramaItem>
<MyUserControl>
</PanoramaItem>
</Panorama>
where MyUserControl is:
<UserControl>
<ListBox>
... lots of "items" possibly created inside a <DataTemplate>
<ListBox>
</UserControl>
If you want to put a list of items inside a ScrollViewer, then you might be better off using an ItemsControl instead.
The XAML you posted for your usercontrol isn't valid (it contains an extra </StackPanel>) and has lots more grids than you should need.
Try this:
<ListBox x:Name="MyListBox" ItemsSource="{Binding MyProperty}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image x:Name="MyImage" Source="{Binding MyImageSource}" VerticalAlignment="Center" HorizontalAlignment="Center" />
<StackPanel>
<TextBlock x:Name="Label" Text="{Binding MyLabel}" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="ReadOnlyTextBlock" Text="{Binding MyStatus}" Style="{StaticResource PhoneTextSubtleStyle}" />
<TextBlock x:Name="PaidTextBlock" Text="{Binding MyPurchase}" Style="{StaticResource PhoneTextSubtleStyle}" Foreground="Blue"/>
</StackPanel>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

Resources