2012-07-25 18 views
0

有沒有在工具提示中使用ContentStringFormat處理多個格式的方法?如何將wpf contentstringformat用於多種格式?

ContentStringFormat="{}{0:N0}{0:P}" 

<Slider.ToolTip> <ToolTip Content="{Binding Path=PlacementTarget.Value, RelativeSource={RelativeSource Mode=Self}}" ContentStringFormat="{}{0:N0}" /> </Slider.ToolTip> 

這就是這樣。當我改變ContentStringFormat這樣的:

ContentStringFormat="{}{0:N0} {0:P}" 

它顯示505,000.00%,而不是50%,提前

+1

我們需要更多的細節。你想實現什麼?你已經嘗試過了什麼?什麼地方出了錯? – stijn 2012-07-25 08:42:12

+0

我想顯示百分比像50%的工具提示數字。我想用{0:P}來顯示百分比。但也有另一種格式,如{0:N0}。如果我用{0:P}替換{0:N0},將顯示百分比,但數字看起來不像預期的那樣。 – bilgestackoverflow 2012-07-25 09:00:31

+0

對不起還沒有足夠的信息 - 請編輯你的答案,顯示你想要的工具提示文本和內容的xaml - 例如,如果你使用Content =「{Binding Path = num}」ContentStringFormat =「{} {0:N0} {0:P}「',num是0.5,這表示'1 50%'是正確的。 – stijn 2012-07-25 09:29:29

回答

1

感謝您在使用standard numeric formatPp顯示的號碼首先乘以100,然後格式化爲具有尾隨百分號的浮點數。在P之後的精度說明符,例如P2控制小數位數。請參閱here

因此格式化的0.5值與格式P2結果字符串「50.00%」:

你似乎做的是爲數字(N0)顯示值50,接着是百分比(P) 。結果是字符串「50 5,000.00%」(因爲P的默認精度爲2)。

2

有三個問題在這裏:

  • 如果它顯示的505,而不是50,你輸入的號碼是5.05,但它應該是0.505
  • 第二,如果你想要0.505顯示爲50%你必須圓首先
  • 最後,你必須使用P0

所以圍捕:

//code 
public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
    InitializeComponent(); 
    DataContext = this; 
    } 

    private double MyActualNumber 
    { 
    get { return 0.505; } 
    } 

    public double Number 
    { 
    get { return System.Math.Round(100.0 * MyActualNumber)/100.0; } 
    } 
} 

//xaml 
<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Label Content="{Binding Path=Number}" ContentStringFormat="{}{0:P0}"/> 
</Window> 
+0

它顯示50爲N0和5000.00%爲P. – Clemens 2012-07-25 10:23:22

+0

不,它沒有。請參閱使用完整代碼進行編輯,以便檢查它。 – stijn 2012-07-25 10:37:29

+0

連接「50」和「5,000.00%」,看看你得到了什麼。 – Clemens 2012-07-25 10:39:17

相關問題