I have MapItemsControl, Pushpins and Data-binding:
<toolkit:MapItemsControl>
<toolkit:MapItemsControl.ItemTemplate>
<DataTemplate>
<toolkit:Pushpin GeoCoordinate="{Binding Coordinate}"
Content="{Binding ContentPushpin}"
Background="{Binding Background}"
Tag="{Binding Tag}"
Tap="userPushpin_Tap"
>
</toolkit:Pushpin>
</DataTemplate>
</toolkit:MapItemsControl.ItemTemplate>
</toolkit:MapItemsControl>
Binding pushpins to MapItemsControl:
ObservableCollection<MyPushpin> PushpinsCollections = ObservableCollection<MyPushpin>();
PushpinsCollections.Add(new MyPushpin()
{
Coordinate = ....,
Id = ...
//init other properties
});
NokiaMapItemsControl.ItemsSource = PushpinsCollections;
Next, one of the Pushpin changed the coordinates, I can find it in the collection by a unique ID. But how can I change his position that these changes appear on the map, without redrawing the other pushpins?
Related
What can I do in the following situation?
I have a simple page. In code behind I add PhoneTextBox control for some filtering of a data. But sometimes (frequently) when I try to write something, I see, that text inside is transparent or collapsed or something else, so I don't see it. I don't see it even when I select this text.
// Generating of a PhoneTextBox
SearchBox = new Microsoft.Phone.Controls.PhoneTextBox();
SearchBox.DataContext = searchBoxContext;
SearchBox.Name = string.Format("SearchBox_{0}", Guid.NewGuid());
SearchBox.Visibility = Visibility.Collapsed;
// Adding Phone text box in a Grid on the Page
RowDefinition rd = new RowDefinition();
rd.Height = GridLength.Auto;
PageDynamicGrid.RowDefinitions.Insert(0, rd);
Grid.SetRow(generator.SearchBox, 0);
foreach (FrameworkElement child in PageDynamicGrid.Children)
{
Grid.SetRow(child, Grid.GetRow(child) + 1);
}
SearchBoxContext = (generator.SearchBox.DataContext as SearchButtonModel);
SearchBoxContext.SearchTextChanged += SearchBoxContext_SearchTextChanged;
generator.SearchBox.TextChanged += SearchBox_TextChanged;
generator.SearchBox.LostFocus += SearchBox_LostFocus;
generator.SearchBox.KeyUp += SearchBox_KeyUp;
generator.SearchBox.DataContext = null;
PageDynamicGrid.Children.Add(generator.SearchBox);
PageDynamicGrid.UpdateLayout();
and page.xaml
<Grid x:Name="PageDynamicGrid" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="{Binding Title}"
Style="{StaticResource PhoneTextTitle1Style}" />
</StackPanel>
<ListBox Grid.Row="1" Margin="12,0"
ItemsSource="{Binding PageDynamicContent, Mode=OneWay}"/>
</Grid>
Almost all content of this page (including search box) creates dynamically, but other content is some buttons and links and I need to filter it, if I have search box. Filter works, but users don't like collapsed text in search box. So it looks like that (and there is not a whitespaces in front of the marker)
So, i don't know why, but it seemed that problem is in the order of actions:
First of all create element, change grid column and row definitions
and add element in the grid.
Update layout of the grid.
And only after all this actions we can collapse PhoneTextBox.
it works for me.
I am binding a collection of images that I retrieve from IsolatedStorage to my View, but I am having issues with changing the order in which images are displayed. There is a timestamp associated with each image, and I would like to be able to sort by either ascending or descending order. As of now the binding works, but when I attempt to change the sort order before binding to my listbox, nothing shows on the UI.
MainPage.xaml
<ListBox x:Name="Recent" ItemsSource="{Binding Pictures}" Margin="8"
SelectionChanged="recent_SelectionChanged" toolkit:TiltEffect.IsTiltEnabled="True"
ItemContainerStyle="{StaticResource MyStyle}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
MainPage.xaml.cs
// Constructor
public MainPage()
{
InitializeComponent();
//This is working, but is not sorted
//DataContext = PictureRepository.Instance;
//Attempt at sorting before binding
//from SettingsPage, if AscendingSort = True then sort ascending
if (Settings.AscendingSort.Value)
{
//Give no errors, but does not display on UI
DataContext = PictureRepository.Instance.Pictures.OrderBy(s => s.DateTaken);
}
else
{
DataContext = PictureRepository.Instance.Pictures.OrderByDescending(s => s.DateTaken);
}
}
Not sure exactly what the issue is here?
Note, when debugging, I can see that PictureRepository.Instance contains the images in which to display in the view.
Try this,
listItems = new ObservableCollection<YourType>(listItems.OrderBy(x => x.DateTaken));
My pushpins are added to the map in a common way like this:
C#:
private readonly ObservableCollection<PushpinModel> _pushpins = new observableCollection<PushpinModel>();
public ObservableCollection<PushpinModel> Pushpins
{
get{return _pushpins;}
}
XAML:
<my:MapItemsControl ItemsSource="{Binding Pushpins}">
<my:MapItemsControl.ItemTemplate>
<DataTemplate>
<my:Pushpin Style="{StaticResource PushpinStyle}"
MouseLeftButtonUp="Pushpin_MouseLeftButtonUp"
Location="{Binding Location}"
Content="{Binding Num}">
</my:Pushpin>
</DataTemplate>
</my:MapItemsControl.ItemTemplate>
</my:MapItemsControl>
On clicking a pushpin i can get the Pushpin as sender and get Location information.
But i would like to track down to it's PushpinModel object to get other informations associated with the Pushpin, like name, description, urls, etc. How can i do that?
First of all, you should use the Tap event, instead of MouseLeftButtonUp.
Secondly, the sender in the event handler, is the Pushpin, and it's DataContext property is your bound PushpinModel, so simply do:
var pushpinModel = (sender as Pushpin).DataContext as PushpinModel;
I want to use a WriteableBitmap to render a programmatically instantiated UserControl to a jpg/png image to use it as a live tile background image in a Windows Phone 7.1 project, but DataBinding is not working as expected when rendering the control.
In general, the UserControl is something like this:
<UserControl>
<Grid x:Name="LayoutRoot" Height="173" Width="173" >
<Grid.Background >
<SolidColorBrush Color="{StaticResource PhoneAccentColor}" />
</Grid.Background >
<Grid.RowDefinitions>
<RowDefinition Height="27"/>
<RowDefinition Height="146"/>
</Grid.RowDefinitions >
<ItemsControl Grid.Row="1" Margin="10,0,0,0" ItemsSource="{Binding}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding MyBindingProperty, FallbackValue=xxx}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<TextBlock Text="Hello World" FontSize="22" Margin="5,0,0,0"/>
<TextBlock TextWrapping="Wrap" Text="{Binding Count, FallbackValue=-1}" FontSize="18.667" Margin="123,0,0,0"/>
</Grid>
</UserControl>
When I now put this control onto a PhoneApplicationPage and assign a list with items of my data structure to the DataContext property of the UserControl, everything works fine and I see one TextBlock for each list item and the Text property of that TextBlock is correctly displaying the value of the property of my data structure. Also the last TextBlock on the Grid displays correctly the current count of list items.
BUT when I'm now trying to programmatically create that UserControl, assign the same list to the DataContext and then use a WriteableBitmap to render it to an image file, it seems that all DataBindings within the DataTemplate of the ItemsControl aren't working anymore, they're displaying the FallbackValue now. Although the DataBinding of the outer TextBlock in the Grid is still working perfectly and also I got the correct number of TextBlocks in the StackPanel (= items in the bound list).
Here is my code for creating the the WriteableBitmap:
var tile = new MyTileControl { DataContext = this._myList };
tile.Arrange(new Rect(0, 0, 173, 173));
tile.Measure(new Size(173, 173));
var bmp = new WriteableBitmap(173, 173);
bmp.Render(tile, null);
bmp.Invalidate();
What's the problem with the DataBindings in the DataTemplate when rendering through a WriteableBitmap and how can I solve it?
I think, that your control not fully created yet, and you can't grab bitmap just after creation. Try to use Dispatcher.BeginInvoke or something else for delayed grabbing of image.
Also, add this control to your page and look where is a problem - in control creation or in bitmap? This give you more information about a problem.
in my app for WP7(mango) I need to navigate the user from one point to another. I know there is the Map control that lets you draw staff on it, but how do you ask it to draw the path for you? (based on the specified destination & user's current location - but that keeps changing so how do you update the route if he goes of the way?)
To update the map with the users current location, use the GeoCoordinateWatcher and update the position of a databound Pushpin as it changes. Remember to set the minimum distance to something low, like 5 meters.
A pushpin like the one on bing maps, can be created with this XAML template:
<maps:Pushpin Background="{StaticResource PushpinLocationBrush}"
Location="{Binding MyLocation}">
<maps:Pushpin.Template>
<ControlTemplate>
<Grid>
<Rectangle Width="15"
Height="15"
Margin="0"
Fill="Black">
<Rectangle.Projection>
<PlaneProjection CenterOfRotationX="0"
LocalOffsetX="-2"
LocalOffsetY="5"
RotationZ="45" />
</Rectangle.Projection>
</Rectangle>
<Ellipse Width="7"
Height="7"
Margin="0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Fill="Orange"
RenderTransformOrigin="0.339,0.232"
StrokeThickness="0" />
</Grid>
</ControlTemplate>
</maps:Pushpin.Template>
</maps:Pushpin>
Getting the GeoCoordinate of a address can be done with Bing Maps. You can read more about Bing Services here: http://msdn.microsoft.com/en-us/library/cc980922.aspx -- the one you need is the GeoCodeService
Drawing a path is rather complicated, specially if you want it to follow the roads. For this, you need the Bing Maps Route Service.
Add the service to Visual Studio, with RouteServiceReference as name, and then you can utilize following code to get the path fragments, and add them to your map. The XAML below reflects the controls I add the fragments to:
List<GeoCoordinate> locations = new List<GeoCoordinate>();
RouteServiceClient routeService = new RouteServiceClient("BasicHttpBinding_IRouteService");
routeService.CalculateRouteCompleted += (sender, e) =>
{
var points = e.Result.Result.RoutePath.Points;
var coordinates = points.Select(x => new GeoCoordinate(x.Latitude, x.Longitude));
var routeColor = Colors.Blue;
var routeBrush = new SolidColorBrush(routeColor);
var routeLine = new MapPolyline()
{
Locations = new LocationCollection(),
Stroke = routeBrush,
Opacity = 0.65,
StrokeThickness = 5.0,
};
foreach (var location in points)
{
routeLine.Locations.Add(new GeoCoordinate(location.Latitude, location.Longitude));
}
RouteLayer.Children.Add(routeLine);
};
RouteBingMap.SetView(LocationRect.CreateLocationRect(locations));
routeService.CalculateRouteAsync(new RouteRequest()
{
Credentials = new Credentials()
{
ApplicationId = "YOURBINGMAPSKEYHERE"
},
Options = new RouteOptions()
{
RoutePathType = RoutePathType.Points
},
Waypoints = new ObservableCollection<Waypoint>(
locations.Select(x => new Waypoint()
{
Location = x.Location
}))
});
Related XAML:
<maps:Map x:Name="RouteBingMap"
AnimationLevel="None"
CopyrightVisibility="Collapsed"
CredentialsProvider="YOURBINGMAPSKEYHERE"
LogoVisibility="Collapsed"
ZoomBarVisibility="Collapsed"
ZoomLevel="12">
<maps:MapLayer x:Name="RouteLayer" />
</maps:Map>