快速的問題。 我有一個驗證器在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");
}
}
}
鏈接幫助,但視覺工作室沒有告訴我任何事情。它不是某種我可以查詢的有感知力的AI。不知道你的意思 – Sergio 2010-11-22 17:27:44
這個作品我同意,但矯枉過正給我。 – zinking 2013-07-12 09:34:28
這是另一種方式:http://stackoverflow.com/questions/7456334/wpf-binding-property-in-validationrule – 2015-08-13 16:07:18