Getting Data from a Data Bound Map View Item's Context Menu - windows-phone-7

I'm writing a Windows Phone application that shows a number of locations on a map. These positions have phone numbers, twitter accounts and websites associated with them.
What I want to do is add a context menu to the items that allows the user to open the websites, view twitter or call the phone number. However, with a MapItemsControl their is no easy way to get the current item.
I am looking for some way to do this in as painless a way as possible.
Some code:
<mi:MapItemsControl x:Name="mapPins" ItemsSource="{Binding Pushpins}">
<mi:MapItemsControl.ItemTemplate>
<DataTemplate>
<mi:Pushpin Location="{Binding Location}">
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu IsEnabled="{Binding ShowContext}" >
<TextBlock Opacity="{Binding Opacity, ElementName=image}" Text="Show Menu"/>
<TextBlock Opacity="{Binding Opacity, ElementName=image1}" Text="Open Webpage" Tap="EOpenWeb" />
<StackPanel Opacity="{Binding Opacity, ElementName=image2}" Orientation="Horizontal" Tap="ECallPhone">
<TextBlock Text="Call "/>
<TextBlock Text="{Binding Contact.FormattedPhone}"/>
</StackPanel>
<TextBlock Opacity="{Binding Opacity, ElementName=image3}" Text="On Twitter" Tap="EOpenTwitter" />
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
<StackPanel HorizontalAlignment="Stretch">
<TextBlock Foreground="{Binding Foreground}" Text="{Binding Name}" d:LayoutOverrides="HorizontalAlignment"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,-6" Visibility="{Binding ShowGlyphs}">
<TextBlock TextWrapping="Wrap" Text="{Binding HereNow}" Visibility="{Binding ShowHere}" VerticalAlignment="Center" FontSize="{StaticResource PhoneFontSizeMediumLarge}" HorizontalAlignment="Left" Height="36" Width="48" TextAlignment="Center"/>
<Image x:Name="image" Height="48" Opacity="{Binding HasMenu}" Source="locationGlyphs/appbar.book.open.png" Stretch="Fill"/>
<Image x:Name="image1" Height="48" Opacity="{Binding HasWeb}" Source="locationGlyphs/appbar.link.png" Stretch="Fill" Width="48"/>
<Image x:Name="image2" Height="48" Opacity="{Binding HasPhone}" Source="locationGlyphs/appbar.phone.png" Stretch="Fill" Width="48"/>
<Image x:Name="image3" Height="48" Opacity="{Binding HasTwitter}" Source="locationGlyphs/appbar.twitter.bird.png" Stretch="Fill" Width="48"/>
</StackPanel>
</StackPanel>
</mi:Pushpin>
</DataTemplate>
</mi:MapItemsControl.ItemTemplate>
</mi:MapItemsControl>
That details how the objects are drawn to the map. The Pushpins list is just an Observable Collection of my data.
If this was a ListBox I could simply use the SelectedItem or similar to resolve this, but that doesn't seem an option for MapItemsControl. Has anyone managed to do this successfully? How?

You need to somehow react to the tap on the pushpin. You have a contextmenu and you clearly need to go that way at the moment. needs to have a tap event handler attached:
<mi:Pushpin
Location="{Binding Location}" Tap="Pushpin_Tap">
Pushpin_Tap event handler needs to be managed similar to this:
private void Pushpin_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
var _ppmodel = sender as Pushpin;
ContextMenu contextMenu =
ContextMenuService.GetContextMenu(_ppmodel);
contextMenu.DataContext = _viewModel.Pushpins.Where
(c => (c.Location
== _ppmodel.Location)).FirstOrDefault();
// this way you can find which pushpin/position was tapped by finding the
// one which has the same location in your Pushpins observable collection
if (contextMenu.Parent == null)
{
contextMenu.IsOpen = true;
}
}
I wrote about showing a context menu on pushpin tap on my blog, so if you wish you can go and read the whole story there.

Related

Windows phone long list selector

i have a long list selector that contains a check box in each list box item.
the long list selector is binded to a list that contain the name the image and the status
I want to check or uncheck the check box depending on the status of the field.
I have done a lot of search and I am not able to achieve this please help.
XAML :
<phone:LongListSelector x:Name="lstBoxRates" Margin="0,27,-12,0" ItemsSource="{Binding Items}" Height="16776941" SelectionChanged="MainLongListSelector_SelectionChanged">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,0,0,17">
<Image Width="54" Height="54">
<Image.Source>
<BitmapImage UriSource="{Binding LocalPath}" />
</Image.Source>
</Image>
<TextBlock Text="{Binding name}" TextWrapping="NoWrap" Style="{StaticResource PhoneTextSubtleStyle}"/>
<CheckBox x:Name="LBCheckBox" Content="{Binding LineOne}" IsChecked="{Binding IsChecked}" Height="75" Width="412" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" />
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
C#:
cView = new CViewModel(AppResources.DBConnectionString);
List<UBCars> ubc = cView.GetAllCars();
lstBoxRates.ItemsSource = ubc;
I tried your code and it works after making these changes:
Removed the ItemsSource binding in xaml. No point in setting it twice
Use properties, not fields, in your model (the UBCars class).

How to change run-time list box image path in wp7

I have one list box and this list box contain image control.
I want to chane run time image path on click event of list box item.
How can i change my image control path ?
My code is like this:
<ListBox x:Name="TransactionList" Width="450" Height="450" Margin="0,0,0,0">
<ListBox.ItemTemplate >
<DataTemplate>
<StackPanel Orientation="Horizontal" Width="450" Height="auto" Margin="0,0,10,0" >
<StackPanel Orientation="Horizontal" Width="300" Height="auto" Margin="0,0,1,10">
<TextBlock Height="35" Width="250" Margin="10,0,0,90" Text="{Binding SONGNAME}" FontSize="22" Foreground="Red" Name="tbSubCategories" />
<TextBlock Height="25" Margin="-250,10,180,30" Text="SingBy:" FontSize="18" Foreground="Gray" Name="tbsingby" />
<TextBlock Height="auto" TextWrapping="Wrap" Width="210" Margin="-180,50,0,70" Text="{Binding SINGBY}" FontSize="18" Foreground="Gray" Name="tbSingBy" />
<TextBlock Height="25" Margin="-280,60,200,0" Text="MusicBy:" FontSize="18" Foreground="Gray" Name="tbMusicby" />
<TextBlock Height="auto" TextWrapping="Wrap" Width="200" Margin="-205,100,0,30" Text="{Binding MUSICBY}" FontSize="18" Foreground="Gray" Name="tbMusicBy" />
</StackPanel>
<StackPanel Orientation="Vertical" Height="auto" Width="auto" Margin="0,50,0,35">
<Image Name="imgsubcatagorie" Width="40" Height="40" VerticalAlignment="Center" FlowDirection="LeftToRight" Source="/VodafoneAugmentedReality;component/Images/play1.png" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I want to change imgsubcatagories image control path when i click on list box item.
Help me
Alright, that's not quite easy.
If can get your image object, that could be easily done like this:
imgsubcatagorie.Source = ModifyImageSource(new Uri("yourUri", UriKind.RelativeOrAbsolute));
private ImageSource ModifyImageSource(Uri uri)
{
image = new BitmapImage(uri);
return image;
}
However, you can't just take this object. The main problem is how to get it.
You should somehow pull it from TransactionList. (also, you have a quite heavy construction: listbox=>stackpanel=>stackpanel=>image)
My first idea was: use loop to go through all items in TransactionList and try to find image there.
Possible solutions
I'm not well-expirienced with ListBox.ItemTemplate feature, and I don't have your whole code, but whatever...
Direct approach: try to use loop to find item.
foreach (var x in TransactionList.Items)
{
if (x is Image)
{
image = (Image)x;
break;
}
}
Then use this image object in event handler.
Also
Find element of concrete type.
Also check this question:
Find control inside Listbox.ItemTemplate
Not for wp7, but may work.

How can I toggle show/hide state of pushpin's infoboxes in Bing Maps, Windows Phone?

It's a simple app. It show dynamic multiple pushpins based on data binding.
When user tap pushpin, the info box will show above as show in the picture.
When user tap the pushpin again. The infobox will collapsed.
And if user tap any area in the map which is not pushpin. All pushpins's infoboxes will collapsed.
The other thing is; when infobox is tapped, it will navigate into the detail page of that location.
This is my resource section.
<phone:PhoneApplicationPage.Resources>
<ControlTemplate x:Key="currentPushPin" TargetType="my:Pushpin">
<Grid x:Name="ContentGrid"
Width="58"
Height="76"
Margin="0">
<Image Source="images/currentPin.png" Stretch="Fill"/>
</Grid>
</ControlTemplate>
<ControlTemplate x:Key="normalPushPin" TargetType="my:Pushpin">
<StackPanel>
<ContentPresenter x:Name="content" HorizontalAlignment="Center"/>
<Path Data="M0,0 L0,1 L1,0"
Fill="Black" Stretch="Fill" Margin="15,0" Height="12"
Width="18" Visibility="{Binding RelativeSource={RelativeSource TemplatedParent},
Path=Content.Visibility, Mode=TwoWay}" HorizontalAlignment="Left" />
<Image Source="images/Pin.png" Stretch="None" HorizontalAlignment="Left"/>
</StackPanel>
</ControlTemplate>
<DataTemplate x:Key="LogoTemplate">
<my:Pushpin Background="#FFB6DE2E" Location="{Binding location}" Tap="Pushpin_Tap">
<my:Pushpin.Content>
<Border Background="Black" Width="130" Visibility="Collapsed" x:Name="border" HorizontalAlignment="Center" >
<StackPanel>
<TextBlock TextAlignment="Center" Text="{Binding title}"/>
</StackPanel>
</Border>
</my:Pushpin.Content>
</my:Pushpin>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
This is my Map and MapItemControl.
<my:Map ZoomLevel="10" Margin="0,-21,0,0" Name="myMap" CredentialsProvider="AsbJ8nhHrawCJNpgYLyPRSunojuLOKcZtMj_ZUEgEGW5dSsczqrQHDZwbi0i2bFY" Tap="map_Tap">
<my:Pushpin Name="currentPin" Template="{StaticResource currentPushPin}" Margin="0,-22,0,0">
</my:Pushpin>
<my:MapItemsControl ItemTemplate="{StaticResource LogoTemplate}" ItemsSource="{Binding PushpinCollection}" >
</my:MapItemsControl>
</my:Map>
The question is what code I should write in these event handlers?
Another question is how can I make infoboxes navigate to other page if it is tapped.
//Event Handler for pushpins
private void Pushpin_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
//What I should write here?
}
private void map_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
//What I should write here?
}
Thanks !!!
I know this ia rather late, but to save some pain for people who have similar issues -
Take a look at http://igrali.com/2012/01/07/show-a-tooltip-for-tapped-pushpin-on-windows-phone/
It may give you some guidance on how to proceed.

Windows Phone 7 - ListBox and Stackpanel sizes

I Have a little problem with a ListBox and its elements. As you can see in my code, the stackpanel for both textblocks is set to 250. This is set like this because otherwise the text expands in 1 line and you cannot see the button. As obvious the problem is that setting this parameter as static, if the resolution or the orientation changes wont fit the screen completely. I would like to know if there is a way to set this width dynamically.
<ListBox x:Name="attractionsListbox" Margin="0,0,-12,0" ItemsSource="{Binding Attractions}" Loaded="attractionsListbox_Loaded">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Tag="{Binding Name}" Tap="AttractionListBoxItem_Tap">
<Image Height="140" Width="140" Margin="0,5,0,0" Source="{Binding Thumbnail}" MaxWidth="140" MaxHeight="140" Stretch="Fill" />
<StackPanel Width="250">
<TextBlock x:Name="AttractionName" Text="{Binding Name}" TextWrapping="Wrap" Style="{StaticResource PhoneTextSmallStyle}" FontSize="20" Foreground="Black" FontWeight="Bold" />
<TextBlock Text="{Binding ShortDescription}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSmallStyle }" Foreground="Black" />
</StackPanel>
<Button Width="80" Height="80" Padding="10,3,10,5" BorderBrush="Black" Click="Add_Todo_Click" Tag="{Binding Name}">
<Button.Background>
<ImageBrush ImageSource="/SundsvallWP7;component/Images/appbar.add.rest.png"/>
</Button.Background>
</Button>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
(In android there is weights, where you can assign the weight of each control, don't know if this exists in Windows Phone).
Thank you very much.
A DockPanel may work better for your scenario than a StackPanel. Put the button on the right and the image on the left, then your middle StackPanel content will flow to fill.
http://www.windowsphonegeek.com/articles/Using-DockPanel-in-WP7.
Alternatively, there are only the two orientations and resolution shouldn't be changing. You could just bind the Width to the Orientation with a value converter.

Scroll PanoramaItem Header in 'wide' PanoramaItem

I'm interested in creating my own 'Hub' Panorama. I've gotten the 'wide' PanoramaItem working, but I'm trying now to mimic the behavior seen in the Marketplace hub, where the PanoramaItem Header scrolls as you move across the hub.
I'm looking for a way for it to smoothly animate to the end of the hub. Has anyone tried this before, or have any suggestions?
I'd imagine it would be something like this:
//OnPanoramaViewChanged
//get X location of viewport
//animate title to X location
However it doesn't appear that the Panorama has a ScrollViewer attached property.
In case you're curious, here's how I made a wide panorama item.
<controls:PanoramaItem ScrollViewer.HorizontalScrollBarVisibility="Visible" Header="movies" Orientation="Horizontal" Width="900">
<controls:PanoramaItem.HeaderTemplate >
<DataTemplate >
<StackPanel>
<TextBlock Foreground="{StaticResource PanoramaHeaderBrush}" Text="{Binding}">
</TextBlock>
</StackPanel>
</DataTemplate>
</controls:PanoramaItem.HeaderTemplate>
<StackPanel>
<!-- line list with image placeholder and text wrapping -->
<ListBox ItemsSource="{Binding Items}" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.Template>
<ControlTemplate>
<ItemsPresenter />
</ControlTemplate>
</ListBox.Template>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="10">
<Grid Background="{StaticResource ControlTitlesInactivePivotBrush}" Width="173" Height="173" >
<TextBlock FontSize="24" Text="Movie Title (2010)" TextWrapping="Wrap" Style="{StaticResource PhoneTextGroupHeaderStyle}"/>
<Rectangle Fill="White" Height="48" Width="48" HorizontalAlignment="Right" VerticalAlignment="Bottom">
<Rectangle.OpacityMask>
<ImageBrush ImageSource="/Test;component/movie_icn.png" />
</Rectangle.OpacityMask>
</Rectangle>
</Grid>
<TextBlock Text="Movie Title:" Margin="12,0,12,0" Foreground="Black" />
<TextBlock Text="The Title" Margin="12,-6,12,0" Foreground="Gray"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</controls:PanoramaItem>
The Silverlight Panorama control doens't support the behaviour you're after or provide the ability to customize it in a way that will let you do it.
If you really want this then you'll need to build your own control from scratch. I would expect this to be more effort than is justified. Just avoid creating a very wide PanoramaItem instead.

Resources