tags:

views:

12

answers:

1

Hi There, Quick question. I have a validator configured in WPF that checks to ensure that a value is within a specific range. This works great. See code below:

<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>

However, I would like to pass the validator another piece of data from the data that is being bound. I assumed that I could just add this to the delcaration of the validator like so:

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

However, is appears that I can't access the SKU value in this way.

Any suggestions?

Thanks,

EDIT May well be worth pointing out that SKU is simply a string declared in my validator, like so:

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");
        }
    }
}
A: 

You have to use dependency property, Visual Studio already told you that.

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

macias