开发者

Attached dependency typeof(List<T>) problem

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

    public const string ValidationRulesPropertyName = "ValidationRules";

    public static List<ValidationRule> GetValidationRules(DependencyObject obj)
    {
        object result = obj.ReadLocalValue(ValidationRulesProperty);
        if (result == DependencyProperty.UnsetValue)
        {
            result = new List<ValidationRule>();
            obj.SetValue(ValidationRulesProperty, result);
        }
        return (List<ValidationRule>)result;
    }


    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), null);

This code removes the single instance of List created in the Metadata and defers the creation of a list until the first time GetValidationRules is called which in turn creates a List if one hasn't already been created.

When using PropertyMetaData only immutable types should be used as a default value.


You could set the default value in the constructor (and not provide one when definining the DependencyProperty, since with collections in that case you practically set a singleton default value).

If the property is used often you avoid that extra comparison in the get accessor (I suppose if that property is rarely used you'd intiantiate the default value in get the first time it's called as suggested in other answer).

Actually this is the pattern suggested by Microsoft at: http://msdn.microsoft.com/en-us/library/cc903961(VS.95).aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜