2009-12-04 110 views
0

我试图创建一个只允许正整数输入的文本框,并且应该自动显示具有文化特定格式的输入(在这种情况下是en-US,所以它应该使用','符号作为分隔符更大的数字)。所以:带自动文化格式的TextBox?

  • 为1000的条目应在文本框中显示为“1000”
  • 1000的条目应显示为这样的,但正确解释...

目前,还没有这种自动格式化是在第一种情况下进行的,而第二种情况会触发我执行的ValidationRule中的一个错误,以检查输入是否正确(使用TryParse检查输入的有效数字)。

我在这里思考全球化和国际化方面的新尴尬,所以我想知道是否有与文化有关的魔法我可以将显示的格式从实际数据中分离出来并自动进行格式化,而进入?

这是从代码隐藏的文本框的XAML:

<TextBox 
       Foreground="{StaticResource WindowForegroundBrush}" 
       Background="{StaticResource EntryFieldBackgroundBrush}" 
       TextWrapping="NoWrap" 
       MaxLines="1" 
       MaxLength="100" 
       Margin="{StaticResource EntryFormTextBoxMargins}" 
       VerticalAlignment="Stretch" 
       RenderTransformOrigin="0.5,0.5" 
       HorizontalAlignment="Stretch" 
       VerticalContentAlignment="Center" 
       MinWidth="300" 
       MinHeight="30" 
       x:Name="PopTxtBox" 
       MaxWidth="300" 
       TextChanged="PopTxtChange"> 
       <Binding 
        Path="locationPopulation"             
        Source="{StaticResource locDT}" 
        UpdateSourceTrigger="PropertyChanged"> 
        <Binding.ValidationRules> 
         <c:PopValidationRule /> 
        </Binding.ValidationRules> 
       </Binding> 

而这里的有效性规则,我写:

public class PopulationValidationRule : ValidationRule 
    { 
     public override ValidationResult Validate(object value, CultureInfo cultureInfo) 
     { 
      UInt64 popValue; 

      if (value == null) 
      { 
       return new ValidationResult(true, null); 
      } 
      else if (!UInt64.TryParse((string)value, NumberStyles.AllowThousands, null, out popValue)) 
      { 
       return new ValidationResult(false, "Value must be a valid number."); 
      } 

      return new ValidationResult(true, null); 
     } 
    } 

顺便说一句,我d像TextBox一样能够显示为空 - 现在,TextBox在加载时显示为'0',而TextBox为空将触发验证错误(即使我允许它在ValidationRule中)。据我所知,当我将TextBox绑定为数字值时,它不允许为空值。有什么办法来处理这个问题?

+0

关于格式。您应该能够在绑定中使用全数字格式化系统。例如,{Binding ...,StringFormat ='\ {0:0;(0); \}'}“,即在()中为负值,零值为零。 en-us/library/0c899ak8.aspx – adrianm 2009-12-05 20:44:52

+0

现在好了,那正是我之后的! ..但是,当我在绑定中使用StringFormat时,出现错误,指出“'StringFormat'转换器无法转换值'0'(type'UInt64');“等 ...我能想到的唯一的事情就是实现一个IValueConverter,但这对我来说似乎很奇怪 - 不应该存在String和一个基本的数字类型?或者是否有其他东西我缺少? – Ryuching 2009-12-06 19:32:05

+0

嗯。我肯定在这里丢失了一些东西。当我在上面的代码中使用绑定时,StringFormat转换器失败了,但是当我删除绑定时,而是包含一个“Text = ....”行和添加绑定和StringFormat转换器,它的工作原理。只要我明确指定转换器的文化...现在想出如何让我的ValidationRules再次运行。 – Ryuching 2009-12-07 18:48:18

回答

0

好吧,我得到它的工作让我满意。

  • 很显然,我必须指定,绑定应用专门给TextBox.Text元(为什么我没有从一开始就这样做我不确定)。之后,StringFormat按照广告方式工作。

  • 我增加了对PreviewLostKeyboardFocus事件,这消除组分隔符,因为我们正在寻找应的值一个相当简单的事件处理程序的格式化 ULONG - 显示格式化,然后通过我们的StringFormat转换器施加,从而所述预期的值不应该有任何这样的格式(如果有的话,验证失败)。该事件还消除了任何可能的前导零以获得良好的度量。

我想上面的自动更正可能会更漂亮的方式来完成,一些正则表达式,巫术,但因为我不是一个特定的品牌houngan,这是我想出了。

谢谢各位的意见和想法!

代码隐藏我的文本框:

<TextBox 
       Foreground="{StaticResource WindowForegroundBrush}" 
       Background="{StaticResource EntryFieldBackgroundBrush}" 
       TextWrapping="NoWrap" 
       MaxLines="1" 
       MaxLength="100" 
       Margin="{StaticResource EntryFormTextBoxMargins}" 
       VerticalAlignment="Stretch" 
       RenderTransformOrigin="0.5,0.5" 
       HorizontalAlignment="Stretch" 
       VerticalContentAlignment="Center" 
       MinWidth="300" 
       MinHeight="30" 
       x:Name="PopTxtBox" 
       MaxWidth="300" PreviewLostKeyboardFocus="PopTxtBoxLeavePreview"           
       > 
       <TextBox.Text> 
        <Binding 
         Path="locationPopulation"             
         Source="{StaticResource locDT}"       
         UpdateSourceTrigger="LostFocus" 
         StringFormat="{}{0:#,#;;}"> 
         <Binding.ValidationRules> 
          <dataTemplates:PopValidationRule /> 
         </Binding.ValidationRules> 
        </Binding> 
       </TextBox.Text> 

      </TextBox> 

的有效性规则:

public class PopValidationRule : ValidationRule 
    { 
     public override ValidationResult Validate(object value, CultureInfo cultureInfo) 
     { 
      ulong popVal; 

      if (!ulong.TryParse((string)value, out popVal) && !(value == null)) 
      { 
       return new ValidationResult(false, "ValidationResult: Not a number."); 
      } 

      return new ValidationResult(true, null); 
     } 
    } 

PreviewLostKeyboard事件:

private void PopTxtBoxLeavePreview(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e) 
    { 
     TextBox senderBox = (TextBox)sender; 

     //remove leading 0:s. 
     senderBox.Text = senderBox.Text.TrimStart('0'); 
     //remove spaces as group separator 
     senderBox.Text = senderBox.Text.Replace(" ", ""); 
     //remove commas as group separator 
     senderBox.Text = senderBox.Text.Replace(",", ""); 
     //remove singlequotes as group separator 
     senderBox.Text = senderBox.Text.Replace("'", "");    
    } 
} 
0

您可能会发现this有用,注重CultureInfo类

This文章包含了很好的解释..

+0

感谢您的指点! – Ryuching 2009-12-05 14:30:42