Skip to content

Commit 8d3b5ae

Browse files
Pull TranslatePoint() calculation into dedicated AP (#3699)
1 parent eb9338c commit 8d3b5ae

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Microsoft.Xaml.Behaviors;
2+
3+
namespace MaterialDesignThemes.Wpf.Behaviors;
4+
5+
public class SmartHintBehavior : Behavior<SmartHint>
6+
{
7+
public static readonly DependencyProperty YOffsetProperty =
8+
DependencyProperty.RegisterAttached("YOffset", typeof(double), typeof(SmartHintBehavior), new PropertyMetadata(0.0));
9+
public static double GetYOffset(DependencyObject obj)
10+
=> (double)obj.GetValue(YOffsetProperty);
11+
public static void SetYOffset(DependencyObject obj, double value)
12+
=> obj.SetValue(YOffsetProperty, value);
13+
14+
private void UpdateSmartHintLocationRecalculationTrigger()
15+
{
16+
if (AssociatedObject?.FloatingTarget is null) return;
17+
18+
double yOffset = AssociatedObject.FloatingTarget.TranslatePoint(new Point(0, 0), AssociatedObject).Y;
19+
AssociatedObject.SetCurrentValue(YOffsetProperty, yOffset);
20+
}
21+
22+
private void HintHostOnLayoutUpdated(object? sender, EventArgs e)
23+
=> UpdateSmartHintLocationRecalculationTrigger();
24+
25+
protected override void OnAttached()
26+
{
27+
base.OnAttached();
28+
AssociatedObject.LayoutUpdated += HintHostOnLayoutUpdated;
29+
}
30+
31+
protected override void OnDetaching()
32+
{
33+
if (AssociatedObject != null)
34+
{
35+
AssociatedObject.LayoutUpdated -= HintHostOnLayoutUpdated;
36+
}
37+
base.OnDetaching();
38+
}
39+
}

src/MaterialDesignThemes.Wpf/Converters/FloatingHintTranslateTransformConverter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Globalization;
1+
using System.Globalization;
22
using System.Windows.Data;
33
using System.Windows.Media;
44

@@ -8,7 +8,7 @@ public class FloatingHintTranslateTransformConverter : IMultiValueConverter
88
{
99
public object? Convert(object?[]? values, Type targetType, object? parameter, CultureInfo culture)
1010
{
11-
if (values is not [double scale, double lower, double upper, SmartHint hint, Point floatingOffset, ..])
11+
if (values is not [double scale, double lower, double upper, SmartHint hint, Point floatingOffset, double yOffset, ..])
1212
{
1313
return Transform.Identity;
1414
}
@@ -43,7 +43,7 @@ double GetFloatingTargetHorizontalOffset()
4343

4444
double GetFloatingTargetVerticalOffset()
4545
{
46-
double offset = hint.FloatingTarget.TranslatePoint(new Point(0, 0), hint).Y;
46+
double offset = yOffset;
4747
offset += hint.InitialVerticalOffset;
4848
offset -= hint.ActualHeight;
4949

src/MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.SmartHint.xaml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
33
xmlns:converters="clr-namespace:MaterialDesignThemes.Wpf.Converters"
44
xmlns:system="clr-namespace:System;assembly=mscorlib"
5-
xmlns:wpf="clr-namespace:MaterialDesignThemes.Wpf">
5+
xmlns:wpf="clr-namespace:MaterialDesignThemes.Wpf"
6+
xmlns:behaviors="clr-namespace:MaterialDesignThemes.Wpf.Behaviors">
67

78
<Style TargetType="{x:Type wpf:SmartHint}">
89
<Style.Resources>
@@ -18,6 +19,13 @@
1819
<converters:FloatingHintTextBlockMarginConverter x:Key="FloatingHintTextBlockMarginConverter" />
1920
<converters:FloatingHintClippingGridConverter x:Key="FloatingHintClippingGridConverter" />
2021
</Style.Resources>
22+
<Setter Property="wpf:BehaviorsAssist.Behaviors">
23+
<Setter.Value>
24+
<wpf:BehaviorCollection>
25+
<behaviors:SmartHintBehavior />
26+
</wpf:BehaviorCollection>
27+
</Setter.Value>
28+
</Setter>
2129
<Setter Property="HorizontalAlignment" Value="Stretch" />
2230
<Setter Property="HorizontalContentAlignment" Value="Left" />
2331
<Setter Property="IsHitTestVisible" Value="False" />
@@ -158,6 +166,7 @@
158166
<Binding Source="{StaticResource NoContentFloatingScale}" />
159167
<Binding Path="." RelativeSource="{RelativeSource TemplatedParent}" />
160168
<Binding Path="FloatingOffset" RelativeSource="{RelativeSource TemplatedParent}" />
169+
<Binding Path="(behaviors:SmartHintBehavior.YOffset)" RelativeSource="{RelativeSource TemplatedParent}" />
161170
<!-- The bindings below are only here to trigger a recalculation - needed to support AcceptsReturn=true scenario for TextBoxes for example as well as "runtime" changes to the desired hint placement -->
162171
<Binding Path="InitialVerticalOffset" RelativeSource="{RelativeSource TemplatedParent}" />
163172
<Binding Path="InitialHorizontalOffset" RelativeSource="{RelativeSource TemplatedParent}" />

0 commit comments

Comments
 (0)