WPF:将值从绑定数据传递到验证规则

时间:2010-08-02 10:59:49

标签: 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>

但是,我想从验证者那里传递另一段数据。我假设我可以将其添加到验证器的演示中,如下所示:

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

但是,似乎我无法以这种方式访问​​SKU值。

有什么建议吗?

谢谢,

EDIT 值得指出的是,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");
        }
    }
}

0 个答案:

没有答案