UI 字段中的区域性格式不正确

Hui Liu-MSFT 48,681 信誉分 Microsoft 外部员工
2024-02-28T08:31:24.5466667+00:00

我正在使用 WPF .NET 6.0 - XAML - C# 10 Entity Framwork Core。我希望我的应用显示正确的日期和数字本地格式。我尝试使用一个数字,但没有办法将昏迷作为小数分隔符。有人可以帮我吗?

in Class:

public double VatRate { get; set; }

in DBcontext:

modelBuilder.Entity<Vat>().Property(v => v.VatRate).HasMaxLength(6);

in App.XAML.cs


using System.Threading;
using System.Globalization;

namespace ATChome
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("it-CH"); (it also doesn't work with "it-IT")
}
}

in WPF Page XAML:


<TextBox x:Name="TxtVatRate1" HorizontalAlignment="Left" Height="25" Margin="4,131,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="71" FontWeight="Normal" MaxLength="6" MaxLines="1" Text="{Binding VatRate, StringFormat={0:F2}, Mode=TwoWay, UpdateSourceTrigger=Explicit}" TextAlignment="Left" VerticalContentAlignment="Center"/>


Note:此问题总结整理于Culture format not correct in UI field

开发人员技术 | .NET | Entity Framework Core
0 个注释 无注释
{count} 票

接受的答案
  1. 匿名
    2024-02-28T09:09:33.77+00:00

    你可以参考下面的代码。

    Xaml:

    xmlns:gl="clr-namespace:System.Globalization;assembly=mscorlib"  
    
       <TextBlock x:Name="TxtVatRate1" Text="{Binding VatRate, StringFormat='f', ConverterCulture={x:Static gl:CultureInfo.CurrentCulture}}" FontSize="20" TextTrimming="CharacterEllipsis" />
    

    Xaml.cs:

    public partial class MainWindow : Window  
        {  
            public double VatRate { get; set; } = 345.67;  
            public MainWindow()  
            {  
                InitializeComponent();  
                DataContext = this;  
            }  
        }
    

    App.xaml.cs:

    public partial class App : Application  
        {  
            static App()  
            {  
    
                CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("it-IT");  
                CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("it-IT");  
            }  
    
        }
    

    enter image description here


    如果答案是正确的,请点击“接受答案”并点赞。 如果您对此答案还有其他疑问,请点击“评论”。

    注意:如果您想接收相关电子邮件,请按照我们的文档中的步骤启用电子邮件通知 此线程的通知。

    0 个注释 无注释

0 个其他答案

排序依据: 非常有帮助

你的答案

问题作者可以将答案标记为“接受的答案”,这有助于用户了解已解决作者问题的答案。