c# - How to use lambda statements to initialize UIPropertyMetaData in WPF -
i know basic, i'm not getting it. wish initialize dependency property dashes double collection using lambda operator.
what proper syntax?
thank you.
public static readonly dependencyproperty dashesproperty = dependencyproperty.register("dashes", typeof(doublecollection), typeof(customtextblock), new uipropertymetadata( () => { doublecollection d = new doublecollection(); d.add(4); d.add(4); return d; } ));
first of should avoid passing default value reference types in dp metadata because shared across instances of containing class didn't intend to.
say, declared 2 instances of customtextblock, both instances refer same list , modification in list transparent both instances. unless explicitly setting constructor or somewhere else.
be careful default values of dp reference types.
anyhow, if still want it, here how done:
new uipropertymetadata(new doublecollection() { 4, 5 }) update:
if pass default value in metadata, doublecollection's gets freezed automatically i.e. can't add/delete collection in case.
var dashescollection = new customtextblock().dashes.add(5); // throw exception. however, if set explicitly constructor, it's not marked frozen , items can added/deleted collection.
var dashescollection = new customtextblock().dashes.add(5); // works fine. so, essence set value in constructor , not in metadata of dp identifier.
Comments
Post a Comment