StringFormat似乎没有反应

问题描述:

我在我的xaml中有以下绑定,我可以看到显示的double值,但StringFormat完全被忽略。StringFormat似乎没有反应

<Label Content="{Binding ByteCount, StringFormat=n}"/> 

ByteCount属性是double类型的。我甚至改变它甚至串起来,它仍然不起作用。

请问可能是什么原因?

更新:

public double ByteCount 
{ 
      get 
      { 
       return CloneHelper.GetSize(this); 
      } 
} 

public static class CloneHelper 
    { 
     public static double GetSize(BookSetViewModel book) 
     { 
      ..... 

      return total; 
     } 
} 
+1

其中是绑定? –

+0

也许你可以在你的问题中包含实际的绑定? (顺便说一句:一个ByteCount属性表示为double !?) –

+0

如果'ByteCount'是一个'int',则可能缺少一个转换器。 –

WPF的Label实际上有一个ContentStringFormat属性,该属性将覆盖任何绑定的StringFormat

套装ContentStringFormat代替

<Label Content="{Binding ByteCount}" ContentStringFormat="n" /> 
+0

你是一个明星雷切尔。惊人的发现,这是它!!! :)这是Silverlight的相同?我需要测试这个... – Houman

+0

@Kave我不确定它是否与Silverlight相同,但我会这么认为。应该很容易编写一个快速测试并找出。 – Rachel

+0

真的很棒。 –

StringFormat酒店,你必须使用特殊的语法。

对于你的例子,你应该使用这个片段。

<Label Content="{Binding ByteCount, StringFormat={}{0:n}}" /> 

Here's一个链接到MSDN页面的的StringFormat属性。

+1

没有,{}只是在那里摆脱大括号。 “StringFormat = n”应该可以工作(并在此工作)。 –

+0

呵呵。谢谢 - 我不知道。 –

+0

我以为你错过了}在结束之前“。但它也不起作用。我不明白... – Houman

不知道是什么原因,也许是因为目标类型(标签是对象),但它适用于TextBlock:

<TextBlock Text="{Binding ByteCount, StringFormat=n}"/>