DiamonDogX

I have a slider and a map control with a dependency property called ZoomLevel. I want the slider value to be binded to the ZoomLevel. When I drag the slider, the zoom level should change and if the zoom level is changed by some other means in the program (e.g. scroll wheel) the slider value should change. I am familiar with data binding but having some trouble here, as I can't seem to get it working correctly. Thanks.


Re: Windows Presentation Foundation (WPF) Slider control binding to a property (two way)

Marlon Grech



can you please post your code... so that I can give you an example





Re: Windows Presentation Foundation (WPF) Slider control binding to a property (two way)

DiamonDogX

I have a Map object with something like the following (it implements INotifyPropertyChanged):

Code Snippet

...

public int ZoomLevel

{

get { return (int)this.GetValue(ZoomLevelProperty); }

set

{

this.SetValue(ZoomLevelProperty, value);

_ZoomLevel = value;

OnPropertyChanged( "ZoomLevel" );

}

}

...

I have a slider in another class that has a member of type Map and I want to bind (two way) its the value to the ZoomLevel:

Code Snippet

<Slider x:Name="zoomSlider" IsSnapToTickEnabled="True" Minimum="1" Maximum="100" Value="{Binding Path=myMap.ZoomLevel, Mode=TwoWay}" VerticalAlignment="Center"

Height="Auto" HorizontalAlignment="Left" Width="100"/>

The user may also zoom in via the scroll wheel. This changes the ZoomLevel, so the slider needs to be updated when this happens as well (hence two-way)




Re: Windows Presentation Foundation (WPF) Slider control binding to a property (two way)

Luc Vo Van

Hi,

Have you called DependencyProperty.Register in your class constructor It is used to make WPF aware of your dependency property.

http://msdn2.microsoft.com/en-us/library/system.windows.dependencyproperty.register.aspx

Luc





Re: Windows Presentation Foundation (WPF) Slider control binding to a property (two way)

DiamonDogX

I have this declared (outside the constructor)

Code Snippet

public static readonly DependencyProperty ZoomLevelProperty =

DependencyProperty.Register("ZoomLevel", typeof(int), typeof(Map), new PropertyMetadata(1));





Re: Windows Presentation Foundation (WPF) Slider control binding to a property (two way)

Sam Bent - MSFT

I suspect the problem is with Path=myMap.ZoomLevel because there is no property named "myMap". I expect what you wanted was to use the Map object named myMap. If this object is part of the tree (so that its name appears in a NameScope), you should say

ElementSource=myMap, Path=ZoomLevel

Or if the object is declared as a resource named myMap, you should say

Source={StaticResource myMap}, Path=ZoomLevel

Both of these have the effect of using the object named "myMap" as the starting point of the binding, and using its ZoomLevel property as the source of values.






Re: Windows Presentation Foundation (WPF) Slider control binding to a property (two way)

Yiling Lai

Dan posted one good article about custom panel, you can find how to bind slider to custom dependency property in his artical.

http://blogs.msdn.com/dancre/archive/2005/10/02/476328.aspx






Re: Windows Presentation Foundation (WPF) Slider control binding to a property (two way)

DiamonDogX

Hmm... he doesn't seem to use the slider anywhere in the procedural code...





Re: Windows Presentation Foundation (WPF) Slider control binding to a property (two way)

Yiling Lai

It is implemented by XAML:

<Slider MinWidth="200" Minimum="50" Maximum="300"
SmallChange="40" Name="_slider" />
<
myapp:TilePanel ChildSize="{Binding ElementName=_slider, Path=Value}">