forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextBlockService.cs
More file actions
95 lines (84 loc) · 3.11 KB
/
Copy pathTextBlockService.cs
File metadata and controls
95 lines (84 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//-----------------------------------------------------------------------
// <copyright file="TextBlockService.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <summary>
// Provides attached properties for TextBlock control.
// </summary>
//-----------------------------------------------------------------------
namespace Microsoft.Management.UI.Internal
{
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Diagnostics.CodeAnalysis;
using System.ComponentModel;
using System.Diagnostics;
/// <summary>
/// Attached property provider to <see cref="TextBlock"/> control.
/// </summary>
[SuppressMessage("Microsoft.MSInternal", "CA903:InternalNamespaceShouldNotContainPublicTypes")]
public static partial class TextBlockService
{
static partial void IsTextTrimmedMonitoringEnabledProperty_PropertyChangedImplementation(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
TextBlock tb = o as TextBlock;
if (tb == null)
{
return;
}
if ((bool)e.OldValue == true)
{
tb.SizeChanged -= OnTextBlockSizeChanged;
}
else
{
tb.SizeChanged += OnTextBlockSizeChanged;
}
}
private static void OnTextBlockSizeChanged(object sender, SizeChangedEventArgs e)
{
var textBlock = (TextBlock)sender;
UpdateIsTextTrimmed(textBlock);
}
static void OnTextBlockPropertyChanged(object sender, EventArgs e)
{
var textBlock = (TextBlock)sender;
UpdateIsTextTrimmed(textBlock);
}
static void UpdateIsTextTrimmed(TextBlock textBlock)
{
Debug.Assert(textBlock != null);
if (textBlock.TextWrapping != TextWrapping.NoWrap || textBlock.TextTrimming == TextTrimming.None)
{
SetIsTextTrimmed(textBlock, false);
}
else
{
SetIsTextTrimmed(textBlock, CalculateIsTextTrimmed(textBlock));
}
}
private static bool CalculateIsTextTrimmed(TextBlock textBlock)
{
if (!textBlock.IsArrangeValid)
{
return GetIsTextTrimmed(textBlock);
}
Typeface typeface = new Typeface(
textBlock.FontFamily,
textBlock.FontStyle,
textBlock.FontWeight,
textBlock.FontStretch);
// FormattedText is used to measure the whole width of the text held up by TextBlock container
FormattedText formattedText = new FormattedText(
textBlock.Text,
System.Threading.Thread.CurrentThread.CurrentCulture,
textBlock.FlowDirection,
typeface,
textBlock.FontSize,
textBlock.Foreground);
return (formattedText.Width > textBlock.ActualWidth);
}
}
}