簡體   English   中英

如何在 XAML 中格式化此字符串?

[英]How can I format this string in XAML?

我正在嘗試將使用 Winforms 中的部件的程序轉換為 WPF。 我在這部分遇到了一些麻煩。 該程序這樣做:

this.textBox.Properties.DisplayFormat.FormatString = "#,#;-#,#;-";

我不太確定這是在做什么,我正在努力將其轉換為 XAML 用於 WPF。 我知道我必須在這里設置它:

Text="{Binding x, Mode=OneWay, StringFormat={}{...}}"/>

但我不確定什么是等價的,因為#,#;-#,#;-不起作用。 謝謝你的幫助: :)

根據WinForms Format Specifiers ,您的原始格式字符串說:

  1. 您希望正十進制數看起來像整數 #,#(例如:1234.5 看起來像 1,234)
  2. 您希望負十進制數看起來像整數 -#,#(例如:-1234.5 看起來像 -1,234)
  3. 您希望零看起來像 - (示例:-)

對於 WPF,您可以使用相同的字符串格式說明符,但您需要在格式前加上{} 這是必需的,否則 WPF 將與其他標記擴展混淆。

這是一個例子:

主.xaml

<StackPanel>
    <TextBlock Text="{Binding Value1, StringFormat={}{0:#,#;-#,#;-}}"/>
    <TextBlock Text="{Binding Value2, StringFormat={}{0:#,#;-#,#;-}}"/>
    <TextBlock Text="{Binding Value3, StringFormat={}{0:#,#;-#,#;-}}"/>
</StackPanel>

Main.xaml.cs

public double Value1 { get; set; }
public double Value2 { get; set; }
public double Value3 { get; set; }
public MainWindow()
{
    InitializeComponent();
    Value1 = 1234567.890f;
    Value2 = -987654.321f;
    Value3 = 0;
    this.DataContext = this;
}

這是最終的 output:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM