Created
September 8, 2015 13:26
-
-
Save devlights/47788640241cd07831f6 to your computer and use it in GitHub Desktop.
[WPF] 文字が見切れたらツールチップで全文字列表示するテキストボックス
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 参考:http://www.codeproject.com/Articles/309927/WPF-TextBox-With-Ellipsis | |
| public class TextBoxEx : System.Windows.Controls.TextBox | |
| { | |
| private string _longText; | |
| private bool _useLongTextForToolTip; | |
| private bool _isExternalChange; | |
| public TextBoxEx() | |
| { | |
| _longText = string.Empty; | |
| _useLongTextForToolTip = true; | |
| _isExternalChange = true; | |
| LayoutUpdated += TextBoxEx_LayoutUpdated; | |
| SizeChanged += TextBoxEx_SizeChanged; | |
| } | |
| public string LongText | |
| { | |
| get { return _longText; } | |
| set | |
| { | |
| _longText = value ?? string.Empty; | |
| PrepareForLayout(); | |
| } | |
| } | |
| public bool UseLongTextForToolTip | |
| { | |
| get { return _useLongTextForToolTip; } | |
| set | |
| { | |
| if (_useLongTextForToolTip != value) | |
| { | |
| _useLongTextForToolTip = value; | |
| if (value) | |
| { | |
| if (ExtentWidth > ViewportWidth || Text != _longText) | |
| { | |
| ToolTip = _longText; | |
| } | |
| } | |
| else | |
| { | |
| if (_longText.Equals(ToolTip)) | |
| { | |
| ToolTip = null; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| protected override void OnTextChanged(TextChangedEventArgs e) | |
| { | |
| if (_isExternalChange) | |
| { | |
| _longText = Text ?? string.Empty; | |
| if (UseLongTextForToolTip) | |
| { | |
| ToolTip = _longText; | |
| } | |
| PrepareForLayout(); | |
| base.OnTextChanged(e); | |
| } | |
| } | |
| protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e) | |
| { | |
| SetText(_longText); | |
| base.OnGotKeyboardFocus(e); | |
| } | |
| protected override void OnLostKeyboardFocus(KeyboardFocusChangedEventArgs e) | |
| { | |
| PrepareForLayout(); | |
| base.OnLostKeyboardFocus(e); | |
| } | |
| void TextBoxEx_SizeChanged(object sender, System.Windows.SizeChangedEventArgs e) | |
| { | |
| if (Math.Abs(e.NewSize.Width - e.PreviousSize.Width) > float.Epsilon) | |
| { | |
| PrepareForLayout(); | |
| } | |
| } | |
| void TextBoxEx_LayoutUpdated(object sender, EventArgs e) | |
| { | |
| if (UseLongTextForToolTip) | |
| { | |
| if (ExtentWidth > ViewportWidth) | |
| { | |
| ToolTip = _longText; | |
| } | |
| else | |
| { | |
| ToolTip = null; | |
| } | |
| } | |
| } | |
| private void PrepareForLayout() | |
| { | |
| SetText(_longText); | |
| } | |
| private void SetText(string value) | |
| { | |
| if (Text != value) | |
| { | |
| _isExternalChange = false; | |
| Text = value; | |
| _isExternalChange = true; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment