Skip to content

Commit 69a3fe1

Browse files
committed
Getting dpi from presentationsource
Change dpi getting logic
1 parent 01a4139 commit 69a3fe1

File tree

4 files changed

+61
-7
lines changed

4 files changed

+61
-7
lines changed

MaterialDesignThemes.Wpf/ComboBoxPopup.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,19 @@ private CustomPopupPlacement[] ComboBoxCustomPopupPlacementCallback(Size popupSi
105105
Point offset)
106106
{
107107
var locationFromScreen = this.PlacementTarget.PointToScreen(new Point(0, 0));
108-
108+
109109
int locationX = (int)locationFromScreen.X % (int)DpiHelper.TransformToDeviceX(SystemParameters.PrimaryScreenWidth);
110110
int locationY = (int)locationFromScreen.Y % (int)DpiHelper.TransformToDeviceY(SystemParameters.PrimaryScreenHeight);
111-
double realOffsetX = (popupSize.Width - targetSize.Width) / 2.0;
112111

113-
double offsetX = DpiHelper.TransformToDeviceX(offset.X);
114-
double defaultVerticalOffsetIndepent = DpiHelper.TransformToDeviceY(DefaultVerticalOffset);
115-
double upVerticalOffsetIndepent = DpiHelper.TransformToDeviceY(UpVerticalOffset);
116-
double downVerticalOffsetIndepent = DpiHelper.TransformToDeviceY(DownVerticalOffset);
112+
var mainVisual = TreeHelper.FindMainTreeVisual(this.PlacementTarget);
113+
double realOffsetX = (popupSize.Width - targetSize.Width) / 2.0;
114+
double offsetX = DpiHelper.TransformToDeviceX(mainVisual, offset.X);
115+
double defaultVerticalOffsetIndepent = DpiHelper.TransformToDeviceY(mainVisual, DefaultVerticalOffset);
116+
double upVerticalOffsetIndepent = DpiHelper.TransformToDeviceY(mainVisual, UpVerticalOffset);
117+
double downVerticalOffsetIndepent = DpiHelper.TransformToDeviceY(mainVisual, DownVerticalOffset);
117118

118-
if (locationX + popupSize.Width - realOffsetX > SystemParameters.PrimaryScreenWidth || locationX + realOffsetX < 0)
119+
if (locationX + popupSize.Width - realOffsetX > SystemParameters.PrimaryScreenWidth
120+
|| locationX + realOffsetX < 0)
119121
{
120122
SetChildTemplateIfNeed(DefaultContentTemplate);
121123

MaterialDesignThemes.Wpf/DpiHelper.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Text;
66
using System.Threading.Tasks;
77
using System.Windows;
8+
using System.Windows.Media;
89

910
namespace MaterialDesignThemes.Wpf
1011
{
@@ -25,6 +26,28 @@ static DpiHelper()
2526
_dpiY = (int)dpiYProperty.GetValue(null, null);
2627
}
2728

29+
public static double TransformToDeviceY(Visual visual, double y)
30+
{
31+
var source = PresentationSource.FromVisual(visual);
32+
if (source != null)
33+
{
34+
return y * source.CompositionTarget.TransformToDevice.M22;
35+
}
36+
37+
return TransformToDeviceY(y);
38+
}
39+
40+
public static double TransformToDeviceX(Visual visual, double x)
41+
{
42+
var source = PresentationSource.FromVisual(visual);
43+
if (source != null)
44+
{
45+
return x * source.CompositionTarget.TransformToDevice.M11;
46+
}
47+
48+
return TransformToDeviceX(x);
49+
}
50+
2851
public static double TransformToDeviceY(double y)
2952
{
3053
return y * _dpiY / StandartDpiY;

MaterialDesignThemes.Wpf/MaterialDesignThemes.Wpf.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@
277277
<Compile Include="RippleAssist.cs" />
278278
<Compile Include="Ripple.cs" />
279279
<Compile Include="TransitionAssist.cs" />
280+
<Compile Include="TreeHelper.cs" />
280281
<Compile Include="Underline.cs" />
281282
<Compile Include="ValidationAssist.cs" />
282283
<EmbeddedResource Include="Properties\Resources.resx">
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Windows;
7+
using System.Windows.Controls.Primitives;
8+
using System.Windows.Media;
9+
10+
namespace MaterialDesignThemes.Wpf
11+
{
12+
internal static class TreeHelper
13+
{
14+
public static Visual FindMainTreeVisual(Visual visual)
15+
{
16+
DependencyObject root = null;
17+
DependencyObject dependencyObject = visual;
18+
19+
while (dependencyObject != null)
20+
{
21+
root = dependencyObject;
22+
dependencyObject = VisualTreeHelper.GetParent(dependencyObject);
23+
}
24+
25+
return root as Visual;
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)