I have one problem. i have dep. prop. of type List..
public const string ValidationRulesPropertyName = "ValidationRules";
public static List<ValidationRule> GetValidationRules(DependencyObject obj)
{
return (List<ValidationRule>)obj.GetValue(ValidationRulesProperty);
}
public static void SetValidationRules(DependencyObject obj, List<ValidationRule> value)
{
obj.SetValue(ValidationRulesProperty, value);
}
public static readonly DependencyProperty ValidationRulesProperty = DependencyProperty.RegisterAttached(
ValidationRulesPropertyName,
typeof(List<ValidationRule>),
typeof(CustomGrid),
new PropertyMetadata(new List<ValidationRule>()));
And now if I set in my custom grid some textboxes and inside one list of ValidationRules
<Grid>
<TextBox x:Name="txt1">
<ValidationRules>
<Validation:SomeValidationRule/>
</ValidationRule>
</TextBox>
<TextBox x:Name="txt2"/>
</Grid>
Ok. Now the problem is when I try to get list of rules for some element.. If have instances of txt1 and txt2 whe I get validation rules they both return instance of SomeValidationRule.
Grid.GetValidationRules(txt1Instance);
and
Grid.GetValidationRules(txt2Instance);
return same list.
Even if try
Grid.GetValidationRules(new TextBox());
I get the same list with SomeValidationRule as only alement in list. So that is strange. If I manually set list to some element then that element have that list that i set but all others element have list that i set up in xaml only for txt1.
Any idea? Thanks!
Try this modification:-
This code removes the single instance of
Listcreated in the Metadata and defers the creation of a list until the first timeGetValidationRulesis called which in turn creates a List if one hasn’t already been created.When using
PropertyMetaDataonly immutable types should be used as a default value.