Skip to content

Commit a2dc3a7

Browse files
Add Minimum and Maximum constraint properties to the PropertySizer control
1 parent d06200f commit a2dc3a7

File tree

3 files changed

+69
-18
lines changed

3 files changed

+69
-18
lines changed

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Sizers/PropertySizer.bind

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,11 @@
4242
<ColumnDefinition Width="Auto"/>
4343
<ColumnDefinition Width="*"/>
4444
</Grid.ColumnDefinitions>
45+
<!-- Note the use of a TwoWay binding here, this is required for this control to work. -->
4546
<controls:PropertySizer Binding="{Binding OpenPaneLength, ElementName=ViewPanel, Mode=TwoWay}"
4647
HorizontalAlignment="Left"
48+
Minimum="52"
49+
Maximum="440"
4750
Visibility="{Binding IsPaneOpen, ElementName=ViewPanel}"/>
4851
<!--In An Application, put your host frame here: <Frame Grid.Column="1"/>-->
4952
<!--Here we show the content as a border for a visual aid -->

Microsoft.Toolkit.Uwp.UI.Controls.Layout/Sizers/PropertySizer/PropertySizer.Events.cs

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public partial class PropertySizer
2020
/// <inheritdoc/>
2121
protected override void OnDragStarting()
2222
{
23+
// We grab the current size of the bound value when we start a drag
24+
// and we manipulate from that set point.
2325
if (ReadLocalValue(BindingProperty) != DependencyProperty.UnsetValue)
2426
{
2527
_currentSize = Binding;
@@ -29,31 +31,44 @@ protected override void OnDragStarting()
2931
/// <inheritdoc/>
3032
protected override bool OnDragHorizontal(double horizontalChange)
3133
{
32-
horizontalChange = IsDragInverted ? -horizontalChange : horizontalChange;
33-
34-
// TODO: Setup a Minimum/Maximum properties to constrain bounds
35-
////if (!IsValidWidth(TargetControl, _currentSize + horizontalChange, ActualWidth))
36-
////{
37-
//// return false;
38-
////}
39-
40-
SetValue(BindingProperty, _currentSize + horizontalChange);
41-
42-
return true;
34+
// We use a central function for both horizontal/vertical as
35+
// a general property has no notion of direction when we
36+
// manipulate it, so the logic is abstracted.
37+
return ApplySizeChange(horizontalChange);
4338
}
4439

4540
/// <inheritdoc/>
4641
protected override bool OnDragVertical(double verticalChange)
4742
{
48-
verticalChange = IsDragInverted ? -verticalChange : verticalChange;
43+
return ApplySizeChange(verticalChange);
44+
}
45+
46+
private bool ApplySizeChange(double newSize)
47+
{
48+
newSize = IsDragInverted ? -newSize : newSize;
4949

50-
////if (!IsValidHeight(TargetControl, _currentSize + verticalChange, ActualHeight))
51-
////{
52-
//// return false;
53-
////}
50+
// We want to be checking the modified final value for bounds checks.
51+
newSize += _currentSize;
5452

55-
SetValue(BindingProperty, _currentSize + verticalChange);
53+
// Check if we hit the min/max value, as we should use that if we're on the edge
54+
if (ReadLocalValue(MinimumProperty) != DependencyProperty.UnsetValue &&
55+
newSize < Minimum)
56+
{
57+
// We use SetValue here as that'll update our bound property vs. overwriting the binding itself.
58+
SetValue(BindingProperty, Minimum);
59+
}
60+
else if (ReadLocalValue(MaximumProperty) != DependencyProperty.UnsetValue &&
61+
newSize > Maximum)
62+
{
63+
SetValue(BindingProperty, Maximum);
64+
}
65+
else
66+
{
67+
// Otherwise, we use the value provided.
68+
SetValue(BindingProperty, newSize);
69+
}
5670

71+
// We're always manipulating the value effectively.
5772
return true;
5873
}
5974
}

Microsoft.Toolkit.Uwp.UI.Controls.Layout/Sizers/PropertySizer/PropertySizer.Properties.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ public bool IsDragInverted
2828
DependencyProperty.Register(nameof(IsDragInverted), typeof(bool), typeof(PropertySizer), new PropertyMetadata(false));
2929

3030
/// <summary>
31-
/// Gets or sets the binding to a <c>double</c> value that the <see cref="PropertySizer"/> is manipulating.
31+
/// Gets or sets a two-way binding to a <c>double</c> value that the <see cref="PropertySizer"/> is manipulating.
3232
/// </summary>
33+
/// <remarks>
34+
/// Note that the binding should be configured to be a <c>TwoWay</c> binding in order for the control to notify the source of the changed value.
35+
/// </remarks>
3336
/// <example>
3437
/// &lt;controls:PropertySizer Binding="{Binding OpenPaneLength, ElementName=ViewPanel, Mode=TwoWay}"&gt;
3538
/// </example>
@@ -44,5 +47,35 @@ public double Binding
4447
/// </summary>
4548
public static readonly DependencyProperty BindingProperty =
4649
DependencyProperty.Register(nameof(Binding), typeof(double), typeof(PropertySizer), new PropertyMetadata(null));
50+
51+
/// <summary>
52+
/// Gets or sets the minimum allowed value for the <see cref="PropertySizer"/> to allow for the <see cref="Binding"/> value. Ignored if not provided.
53+
/// </summary>
54+
public double Minimum
55+
{
56+
get { return (double)GetValue(MinimumProperty); }
57+
set { SetValue(MinimumProperty, value); }
58+
}
59+
60+
/// <summary>
61+
/// Identifies the <see cref="Minimum"/> dependency property.
62+
/// </summary>
63+
public static readonly DependencyProperty MinimumProperty =
64+
DependencyProperty.Register(nameof(Minimum), typeof(double), typeof(PropertySizer), new PropertyMetadata(0));
65+
66+
/// <summary>
67+
/// Gets or sets the maximum allowed value for the <see cref="PropertySizer"/> to allow for the <see cref="Binding"/> value. Ignored if not provided.
68+
/// </summary>
69+
public double Maximum
70+
{
71+
get { return (double)GetValue(MaximumProperty); }
72+
set { SetValue(MaximumProperty, value); }
73+
}
74+
75+
/// <summary>
76+
/// Identifies the <see cref="Maximum"/> dependency property.
77+
/// </summary>
78+
public static readonly DependencyProperty MaximumProperty =
79+
DependencyProperty.Register(nameof(Maximum), typeof(double), typeof(PropertySizer), new PropertyMetadata(0));
4780
}
4881
}

0 commit comments

Comments
 (0)