c# - 工具提示文化是错误的

标签 c# wpf number-formatting

我在 xaml 中有:

<TextBlock Text="{local:Bind Test}" ToolTip="{local:Bind Test}" />

这是屏幕截图(使用放大镜):

我的问题是这里发生了什么?为什么工具提示显示不同的值(小数点是 ., 是预期的)?


长话短说:

我正在尝试以与用户 Windows 数字格式首选项中相同的格式显示数字。

为此,我在显示窗口之前覆盖了语言(覆盖 App.OnStartup):

FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement),
    new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));

并使用以下自定义绑定(bind)(默认设置转换器文化)

public class Bind : Binding
{
    public Bind(string path) : base(path)
    {
        ConverterCulture = CultureInfo.CurrentCulture;
    }
}

它适用于 TextBoxText 属性,但不适用于 ToolTip

要实际查看我在屏幕截图上显示的内容:

  • 转到 (Windows 7) Control Panel/Region and Language/Formats 并将 Format 设置为 English (United States)
  • 转到 Additional settings/Numbers 并将 Decimal symbol. 更改为 ,
  • 创建新的 wpf 应用程序,复制 xaml,添加语言覆盖,添加转换器并设置:

public partial class MainWindow : Window
{
    public double Test { get; set; } = 1.234567;

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
    }
}

最佳答案

我也遇到了同样的问题。因此,您可以通过在 ToolTip 中添加 TextBlock 并为此 ToolTip 的 TextBlock 绑定(bind)相同的 Text="{local:Bind Test}" 来解决此问题还有。

<TextBlock>
    <TextBlock.ToolTip>
        <TextBlock Text="{local:Bind Test}"/>
    </TextBlock.ToolTip>
</TextBlock>

关于c# - 工具提示文化是错误的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37182987/

相关文章:

c# - 更新剩余的敌人数量 - Unity

c# - 使用 StringFormat 在值中间添加单词

android - 从联系人中选择一个号码但删除国家代码

PHP sprintf格式数字类似于number_format

jquery - 每三位数字加逗号

c# - 无法调用方法,因为参数通过更新 mvc 时的引用异常传递

c# - 为什么没有捕获异常?

excel - 如何以及何时关闭在 Office VSTO 插件中的另一个线程上运行的 WPF 窗口

C# WPF OnPropertyChanged 在 foreach 循环中

WPF ShowDialog 和 ElementHost