2010-08-02 34 views
5

快速的问题。 我有一个验证器在WPF中配置,检查以确保值在特定范围内。这很好。见下面的代码:WPF:将值从绑定数据传递给验证规则

<TextBox Name="valueTxt" Style="{StaticResource SquareBox}" GotKeyboardFocus="amountTxt_GotKeyboardFocus" GotMouseCapture="amountTxt_GotMouseCapture" LostFocus="boxLostFocus" Height="25" Width="50"> 
          <TextBox.Text> 
           <Binding Path="UnitCost" NotifyOnValidationError="True"> 
            <Binding.ValidationRules> 
             <local:ValidDecimal MaxAmount="1000"></local:ValidDecimal> 
            </Binding.ValidationRules> 
            <Binding.Converter> 
             <local:CurrencyConverter addPound="False" /> 
            </Binding.Converter> 
           </Binding> 
          </TextBox.Text> 
         </TextBox> 

但是,我想通过验证程序另一块数据从绑定的数据。我以为我可以将其添加到验证的delcaration像这样:

<local:ValidDecimal MaxAmount="1000" SKU="{Binding Path=tblProducts.ProductSKU}"></local:ValidDecimal> 

然而,似乎是我不能以这种方式访问​​的SKU值。

有什么建议吗?

感谢,

编辑 可能是值得指出的是,SKU简直就是我的验证声明的字符串,像这样:

public class ValidDecimal : ValidationRule 
{ 
    public int MaxAmount { get; set; } 
    public string SKU { get; set; } 

    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo) 
    { 
     //validate the value as a decimal in to two decimal places 
     string cost = (string)value; 
     try 
     { 

      decimal decCost = decimal.Parse(cost); 
      if (SKU != "85555") 
      { 
       if (decCost <= 0 || decCost > MaxAmount) 
       { 
        return new ValidationResult(false, "Amount is not within valid range"); 
       } 
      } 
      return new ValidationResult(true, null); 

     } 
     catch (Exception ex) 
     { 
      return new ValidationResult(false, "Not a valid decimal value"); 
     } 
    } 
} 

回答

4

你必须使用依赖属性时,Visual Studio已经告诉你那个。

阅读:http://dedjo.blogspot.com/2007/05/fully-binded-validation-by-using.html

+6

链接帮助,但视觉工作室没有告诉我任何事情。它不是某种我可以查询的有感知力的AI。不知道你的意思 – Sergio 2010-11-22 17:27:44

+0

这个作品我同意,但矫枉过正给我。 – zinking 2013-07-12 09:34:28

+0

这是另一种方式:http://stackoverflow.com/questions/7456334/wpf-binding-property-in-validationrule – 2015-08-13 16:07:18

相关问题