Reputation: 3
I am making a Bing Map program using C# WPF MVVM.
Whenever the map view in Bing Map changes, I want to bind a BoundingRectangle to the ViewModel and save it.
But BoundingRectangle is just a setter.
So, I am trying to connect an event rather than a general Command binding and send it as a CommandParameter.
I wrote below
Xaml
<Grid Grid.Row="0" Grid.Column="1">
<bm:Map x:Name="bm"
Margin="0,10,10,10"
CredentialsProvider="..."
Mode="AerialWithLabels"
BorderBrush="{StaticResource DefalutBorderBrush}"
BorderThickness="1"
Center="{Binding CenterLocation, Mode=TwoWay,Converter={StaticResource Converter}}"
ZoomLevel="{Binding CurrentZoomLevel, Mode=TwoWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="ViewChangeOnFrame">
<i:InvokeCommandAction Command="{Binding ViewChangedCommand, Mode=OneWay, Converter={StaticResource TestConverter}}"
CommandParameter="{Binding BoundingRectangle, Mode=OneWay, ElementName=bm, Converter={StaticResource ConverterRect}}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</bm:Map>
View Model
public class TestViewModel
{
private double[] _boundingBox;
public double[] BingMapBoundingBox
{
get
{
return _boundingBox;
}
set
{
SetProperty(ref _boundingBox, value);
}
}
public ICommand ViewChangedCommand
{
get;
set;
}
public TestViewModel()
{
ViewChangedCommand = new RelayCommand<double[]>(ViewChanged);
}
public void ViewChanged(double[] boundingBox)
{
BingMapBoundingBox = boundingBox;
}
}
Converter
public class LocationRectToDoubleArrayConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
double[] resultArry = new double[4] { 0, 0, 0, 0 };
if (value == null)
return resultArry;
LocationRect currLocation = value as LocationRect;
resultArry[0] = currLocation.Northwest.Longitude;
resultArry[1] = currLocation.Northwest.Latitude;
resultArry[2] = currLocation.Southeast.Longitude;
resultArry[3] = currLocation.Southeast.Latitude;
return resultArry;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
LocationRect resultLocation = new LocationRect();
if (value is null)
return resultLocation;
double[] dblArray = value as double[];
resultLocation.Northwest.Longitude = dblArray[0];
resultLocation.Northwest.Latitude = dblArray[1];
resultLocation.Southeast.Longitude = dblArray[2];
resultLocation.Southeast.Latitude = dblArray[3];
return resultLocation;
}
}
The event is executed every time the View is changed, but CommandParameter is executed only once when the program is first executed, and is not executed after that.
Whenever the View changes, I want the BoundingRectangle to come in as a CommandParameter. What should I do?
Sorry for the lack of explanation as English is not my native language.
Thank You
Upvotes: 0
Views: 68