How to bind maximum and minimum zoom levels in Bin

2019-05-11 11:45发布

I would like to implement a custom zoom slider for a Bing Maps control in my silverlight application. I can set up the mechanics of it in a pretty straightforward way:

<Slider ... Value="{Binding ZoomLevel, Mode=TwoWay, ElementName=MyMap}"/>

However, I can't find a way to bind the Maximum and Minimum properties of the Slider to the maximum and minimum available zoomlevel for the map. I am using the default views (Road, Aerial) and no custom tilesets or anything fancy.

I've tried something like this:

Maximum="{Binding Path=Map.Mode.ZoomRange.To, Mode=TwoWay}"
Minimum="{Binding Path=Map.Mode.ZoomRange.From, Mode=TwoWay}"

But that doesn't work. When my map loads, the Minimum value is set to 0.0, and the Maximum value is set to 1.0. The actual values of Map.Mode.ZoomRange are 1.0 and 17.0 (for the particular location I am looking at now. The Max changes depending on where the map is centered).

How can I bind these properties to my new custom slider?

2条回答
Bombasti
2楼-- · 2019-05-11 12:05

You won't be able to bind like this because Mode is not a dependency property nor is MapMode a dependency object. Neither are any of the other properties in the chain nor do they implement INotifyPropertyChanged. So binding these is pretty much doomed.

You will need some code. It might be that the Map's ModeChanged event fires when the range is changed, in which case you can update the the Min/Max at the point.

Failing that use one of the view changed events like TargetViewChanged or ViewChangeEnd.

查看更多
姐就是有狂的资本
3楼-- · 2019-05-11 12:16

As stated elsewhere, you can't do this via binding, at least not using the current API.

The following code will do the trick. Pop it just after your InitializeComponent(); statement.

Action updateSliderRange = () =>
    {
        _slider.Minimum = _map.Mode.ZoomRange.From;
        _slider.Maximum = _map.Mode.ZoomRange.To;
    };
_map.ModeChanged += delegate { updateSliderRange(); };
_map.TargetViewChanged += delegate { updateSliderRange(); };
updateSliderRange();
查看更多
登录 后发表回答