我是WPF的新手,對綁定集合的驗證存在問題。如何實現綁定集合的綁定驗證
我有DataGrid綁定到多個對象組合在一起的集合。
數據網格具有兩列:
- 拉布勒一次性界,顯示名稱當文本框引發LostFocus
數據網格中的每一行爲界
我可以綁定基於ExceptionValidationRule的驗證,但性能不是很好,應用程序閃爍,因爲它有很多例外。我也知道我可以這樣做,拋出TextBox LostFocus的事件並獲取源對象,但我試圖對有界對象使用WPF驗證(如果可能的話)。
我試圖繼承有效性規則的基類,並通過它,但它與基類的函數輸入,而不是與派生界項目 我試圖創建實現的有效性規則和DependencyObject的單獨的類和我試圖傳遞給它像這裏解釋的源對象:http://dedjo.blogspot.com/2007/05/fully-binded-validation-by-using.html
但我仍然無法讓它工作。爲了簡單起見,我創建了一個具有TestString屬性的DependencyObject,並試圖將其綁定到顯示名稱(Label的有界路徑)。但是TestString的集合永遠不會被調用。
我該怎麼做?我怎樣才能調用適當的驗證方法?我能否以某種方式將有界對象傳遞給Valuator?
感謝您的幫助, 沙立
我使用的代碼:
實現類的DependencyObject:
class ManifestPropertyDependency : DependencyObject
{
private ManifestProperty _manifestPropertyInstance;
public ManifestProperty ManifestPropertyInstance
{
get { return (ManifestProperty)GetValue(ManifestPropertyInstanceProp); }
set
{
SetValue(ManifestPropertyInstanceProp, value);
}
}
private string testString;
public string TestString {
get { return (string)GetValue(TestStringProp); }
set { SetValue(TestStringProp, value); }
}
public ManifestPropertyDependency()
{
testString = "";
_manifestPropertyInstance = new ManifestProperty();
}
public static readonly DependencyProperty ManifestPropertyInstanceProp =
DependencyProperty.Register("ManifestPropertyInstance", typeof(ManifestProperty),
typeof(ManifestPropertyDependency), new UIPropertyMetadata(new ManifestProperty()));
public static readonly DependencyProperty TestStringProp =
DependencyProperty.Register("TestString", typeof(string),
typeof(ManifestPropertyDependency), new UIPropertyMetadata(""));
}
實現類有效性規則的:
class ManifestPropertyValidator : ValidationRule
{
private ManifestProperty _manifest;
private ManifestPropertyDependency _manifestPropertyDependency;
public string Stam { get { return _manifestPropertyDependency.TestString; } set { _manifestPropertyDependency.TestString = value; } }
public ManifestPropertyDependency ManifestPropertyDependency
{
get { return _manifestPropertyDependency; }
set
{
_manifestPropertyDependency = value;
Stam = value.TestString;
_manifest = value.ManifestPropertyInstance;
}
}
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
try
{
string errorMessage = "";
if (ManifestPropertyDependency.ManifestPropertyInstance.ValidateString((string)value, ref errorMessage))
{
return new ValidationResult(true, null);
}
return new ValidationResult(false, errorMessage);
}
catch (Exception e)
{
return new ValidationResult(false, "Illegal characters or " + e.Message);
}
}
}
XAML中的驗證:
<TextBox Validation.ErrorTemplate="{StaticResource validationTemplate}"
Style="{StaticResource textStyleTextBox}">
<TextBox.Text>
<Binding Path="Value" Mode="TwoWay"
UpdateSourceTrigger="LostFocus">
<Binding.ValidationRules>
<Classes:ManifestPropertyValidator>
<Classes:ManifestPropertyValidator.ManifestPropertyDependency>
<Classes:ManifestPropertyDependency TestString="{Binding Path=DisplayName}"/>
</Classes:ManifestPropertyValidator.ManifestPropertyDependency>
</Classes:ManifestPropertyValidator>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
在類ManifestPropertyDependency中,您爲依賴項屬性定義了支持字段「_manifestPropertyInstance」和「testString」。這是錯誤的! DP不需要支持字段。他們的getter和setter調用'DependencyObject.GetValue'和'DependencyObject.GetValue'來代替(就像你做的那樣)。像在構造函數中那樣,將值分配給未使用的後備字段是毫無意義的。改爲設置屬性。而且,當您在XAML中使用這些DP時,WPF **不會**調用getter或setter,而是直接訪問底層DP。 – Clemens 2012-02-09 09:47:49
我知道支持字段不是nessesary,我只是爲了調試。我試圖瞭解DependencyObject中發生了什麼。應該從ManifestPropertyValidator調用DependencyObject的獲取和設置,並試圖查看這些字段中的內容。 – user1197547 2012-02-09 12:35:03