gpt4 book ai didi

Wpf 小数样式

转载 作者:行者123 更新时间:2023-12-04 02:08:18 25 4
gpt4 key购买 nike

我想用这样的小数位设置文本框的样式:

enter image description here

我该怎么做?

最佳答案

您可以像这样扩展 TextBox

using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;

public class DecimalTextBox : TextBox
{
public static readonly DependencyProperty FloatColorProperty = DependencyProperty.Register("FloatColor", typeof(Color), typeof(DecimalTextBox), new FrameworkPropertyMetadata(Colors.Red));
public Color FloatColor
{
get { return (Color)GetValue(FloatColorProperty); }
set { SetValue(FloatColorProperty, value); }
}

protected TextBlock _textBlock;
protected FrameworkElement _textBoxView;

public DecimalTextBox()
{
_textBlock = new TextBlock() { Margin = new Thickness(1, 0, 0, 0) };
Loaded += ExTextBox_Loaded;
}

private void ExTextBox_Loaded(object sender, RoutedEventArgs e)
{
Loaded -= ExTextBox_Loaded;

// hide the original drawing visuals, by setting opacity on their parent
var visual = this.GetChildOfType<DrawingVisual>();
_textBoxView = (FrameworkElement)visual.Parent;
_textBoxView.Opacity = 0;

// add textblock to do the text drawing for us
var grid = this.GetChildOfType<Grid>();
if (grid.Children.Count >= 2)
grid.Children.Insert(1, _textBlock);
else
grid.Children.Add(_textBlock);
}

protected override void OnLostKeyboardFocus(KeyboardFocusChangedEventArgs e)
{
base.OnLostKeyboardFocus(e);

_textBoxView.Opacity = 0;
_textBlock.Visibility = Visibility.Visible;
}

protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
{
base.OnGotKeyboardFocus(e);

_textBoxView.Opacity = 1;
_textBlock.Visibility = Visibility.Collapsed;
}

protected override void OnTextChanged(TextChangedEventArgs e)
{
base.OnTextChanged(e);

// making sure text on TextBlock is updated as per TextBox
var dotPos = Text.IndexOf('.');
var textPart1 = dotPos == -1 ? Text : Text.Substring(0, dotPos + 1);
var textPart2 = (dotPos == -1 || dotPos >= (Text.Length-1)) ? null : Text.Substring(dotPos + 1);

_textBlock.Inlines.Clear();
_textBlock.Inlines.Add(new Run {
Text = textPart1,
FontFamily = FontFamily,
FontSize = FontSize,
Foreground = Foreground });

if (textPart2 != null)
_textBlock.Inlines.Add(new Run {
Text = textPart2,
FontFamily = FontFamily,
TextDecorations = System.Windows.TextDecorations.Underline,
BaselineAlignment = BaselineAlignment.TextTop,
FontSize = FontSize * 5/6,
Foreground = new SolidColorBrush(FloatColor) });
}
}

public static class HelperExtensions
{
public static T GetChildOfType<T>(this DependencyObject depObj) where T : DependencyObject
{
if (depObj == null) return null;

for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
var child = VisualTreeHelper.GetChild(depObj, i);

var result = (child as T) ?? GetChildOfType<T>(child);
if (result != null) return result;
}
return null;
}
}

XAML 代码使用

<local:DecimalTextBox FloatColor="Maroon" />

你的输出应该是这样的:

screenshot

05/17 更新

说明:正如您从图像中看到的那样,DecimalTextBox 仅在未获得焦点时才以格式化模式显示文本。

我最初开发的控件是在编辑期间支持格式化的(这仍然可以通过注释方法 OnLostKeyboardFocusOnGotKeyboardFocus 来完成) - 但由于字体 -尺寸差异导致光标定位略微倾斜,这反过来又会转化为糟糕的用户体验。

cursor issue

因此,在 GotFocusLostFocus 期间实现交换逻辑来解决这个问题。

关于Wpf 小数样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43824977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com