@@ -68,6 +68,12 @@ public partial class ZoomContentControl : ContentControl
68
68
public static readonly DependencyProperty HorizontalMinScrollProperty =
69
69
DependencyProperty . Register ( nameof ( HorizontalMinScroll ) , typeof ( double ) , typeof ( ZoomContentControl ) , new PropertyMetadata ( 0.0d ) ) ;
70
70
71
+ public static readonly DependencyProperty HorizontalScrollValueProperty =
72
+ DependencyProperty . Register ( nameof ( HorizontalScrollValue ) , typeof ( double ) , typeof ( ZoomContentControl ) , new PropertyMetadata ( 0.0d ) ) ;
73
+
74
+ public static readonly DependencyProperty VerticalScrollValueProperty =
75
+ DependencyProperty . Register ( nameof ( VerticalScrollValue ) , typeof ( double ) , typeof ( ZoomContentControl ) , new PropertyMetadata ( 0.0d ) ) ;
76
+
71
77
public static readonly DependencyProperty ViewPortWidthProperty =
72
78
DependencyProperty . Register ( nameof ( ViewPortWidth ) , typeof ( double ) , typeof ( ZoomContentControl ) , new PropertyMetadata ( 0.0d ) ) ;
73
79
@@ -166,6 +172,19 @@ public double HorizontalMinScroll
166
172
get => ( double ) GetValue ( HorizontalMinScrollProperty ) ;
167
173
set => SetValue ( HorizontalMinScrollProperty , value ) ;
168
174
}
175
+
176
+ public double HorizontalScrollValue
177
+ {
178
+ get => ( double ) GetValue ( HorizontalScrollValueProperty ) ;
179
+ set => SetValue ( HorizontalScrollValueProperty , value ) ;
180
+ }
181
+
182
+ public double VerticalScrollValue
183
+ {
184
+ get => ( double ) GetValue ( VerticalScrollValueProperty ) ;
185
+ set => SetValue ( VerticalScrollValueProperty , value ) ;
186
+ }
187
+
169
188
public double ViewPortHeight
170
189
{
171
190
get => ( double ) GetValue ( ViewPortHeightProperty ) ;
@@ -194,20 +213,11 @@ private void RegisterPropertyHandlers()
194
213
RegisterPropertyChangedCallback ( IsActiveProperty , IsActiveChanged ) ;
195
214
}
196
215
197
- private void UpdateVerticalScrollBarValue ( DependencyObject sender , DependencyProperty dp )
198
- {
199
- if ( _scrollV is not null )
200
- {
201
- _scrollV . Value = VerticalOffset ;
202
- }
203
- }
204
- private void UpdateHorizontalScrollBarValue ( DependencyObject sender , DependencyProperty dp )
205
- {
206
- if ( _scrollH is not null )
207
- {
208
- _scrollH . Value = HorizontalOffset ;
209
- }
210
- }
216
+ //Slide move is always on the opposite direction of the drag
217
+ private void UpdateVerticalScrollBarValue ( DependencyObject sender , DependencyProperty dp ) => VerticalScrollValue = - 1 * VerticalOffset ;
218
+
219
+ //Slide move is always on the opposite direction of the drag
220
+ private void UpdateHorizontalScrollBarValue ( DependencyObject sender , DependencyProperty dp ) => HorizontalScrollValue = - 1 * HorizontalOffset ;
211
221
212
222
private void IsActiveChanged ( DependencyObject sender , DependencyProperty dp )
213
223
{
@@ -216,16 +226,23 @@ private void IsActiveChanged(DependencyObject sender, DependencyProperty dp)
216
226
ResetOffset ( ) ;
217
227
ResetZoom ( ) ;
218
228
}
229
+ if ( _scrollH is not null )
230
+ {
231
+ _scrollH . Visibility = IsActive ? Visibility . Visible : Visibility . Collapsed ;
232
+ }
233
+ if ( _scrollV is not null )
234
+ {
235
+ _scrollV . Visibility = IsActive ? Visibility . Visible : Visibility . Collapsed ;
236
+ }
219
237
}
220
238
221
239
private void UpdateScrollLimits ( )
222
240
{
223
- //min and max are inverted to slide on the opposite direction of the drag
224
- HorizontalMinScroll = this . ActualWidth * ZoomLevel ;
225
- VerticalMinScroll = this . ActualHeight * ZoomLevel ;
241
+ HorizontalMaxScroll = this . ActualWidth * ZoomLevel ;
242
+ VerticalMaxScroll = this . ActualHeight * ZoomLevel ;
226
243
227
- HorizontalMaxScroll = - 1 * HorizontalMinScroll ;
228
- VerticalMaxScroll = - 1 * VerticalMinScroll ;
244
+ HorizontalMinScroll = - 1 * HorizontalMaxScroll ;
245
+ VerticalMinScroll = - 1 * VerticalMaxScroll ;
229
246
}
230
247
231
248
private void CoerceZoomLevel ( DependencyObject sender , DependencyProperty dp )
@@ -261,11 +278,12 @@ protected override void OnApplyTemplate()
261
278
_scrollH = GetTemplateChild ( "PART_scrollH" ) as ScrollBar ;
262
279
263
280
RegisterToControlEvents ( ) ;
281
+
264
282
ResetOffset ( ) ;
265
283
ResetZoom ( ) ;
266
284
RegisterPointerHandlers ( ) ;
267
285
}
268
-
286
+ #region ScrollBars Events
269
287
private void RegisterToControlEvents ( )
270
288
{
271
289
//due to templatebinding there's no TwoWay mode. We need to manually update the values
@@ -282,31 +300,16 @@ private void RegisterToControlEvents()
282
300
283
301
private void _scrollV_Scroll ( object sender , ScrollEventArgs e )
284
302
{
285
- VerticalOffset = e . NewValue ;
303
+ //TemplateBinding doesn't support TwoWay mode. We need to manually update the values
304
+ VerticalOffset = - 1 * e . NewValue ;
286
305
}
287
306
288
307
private void _scrollH_Scroll ( object sender , ScrollEventArgs e )
289
308
{
290
- HorizontalOffset = e . NewValue ;
309
+ //TemplateBinding doesn't support TwoWay mode. We need to manually update the values
310
+ HorizontalOffset = - 1 * e . NewValue ;
291
311
}
292
-
293
- //private void ApplyBindings()
294
- //{
295
- // if (_scrollV is not null)
296
- // {
297
- // var sVBinding = new Binding { Path = new PropertyPath("VerticalOffset"), Mode = BindingMode.TwoWay };
298
- // sVBinding.Source = this;
299
- // _scrollV.SetBinding(ScrollBar.ValueProperty, sVBinding);
300
- // }
301
-
302
- // if (_scrollH is not null)
303
- // {
304
- // var sHBinding = new Binding { Path = new PropertyPath("HorizontalOffset"), Mode = BindingMode.TwoWay };
305
- // sHBinding.Source = this;
306
- // _scrollH.SetBinding(ScrollBar.ValueProperty, sHBinding);
307
- // }
308
- //}
309
-
312
+ #endregion
310
313
311
314
private uint _capturedPointerId ;
312
315
private Point _referencePosition ;
@@ -438,20 +441,14 @@ private double GetPanDelta(PointerPointProperties pointerProperties)
438
441
439
442
public void ResetZoom ( )
440
443
{
441
- if ( IsAllowedToWork )
442
- {
443
- ZoomLevel = 1 ;
444
- HorizontalZoomCenter = 0 ;
445
- VerticalZoomCenter = 0 ;
446
- }
444
+ ZoomLevel = 1 ;
445
+ HorizontalZoomCenter = 0 ;
446
+ VerticalZoomCenter = 0 ;
447
447
}
448
448
449
449
public void ResetOffset ( )
450
450
{
451
- if ( IsAllowedToWork )
452
- {
453
- HorizontalOffset = 0 ;
454
- VerticalOffset = 0 ;
455
- }
451
+ HorizontalOffset = 0 ;
452
+ VerticalOffset = 0 ;
456
453
}
457
454
}
0 commit comments