Wednesday, 15 June 2011

xaml - ContentControl and static ressources -



xaml - ContentControl and static ressources -

i seek extend app missing characterellipsis textblocks in windows phone 8 app. found nice solution here: http://nerdplusart.com/texttrimming-textblock-for-silverlight/.

so implemented in code:

public class dynamictextblock : contentcontrol { #region text (dependencyproperty) /// <summary> /// gets or sets text dependencyproperty. text displayed. /// </summary> public string text { { homecoming (string)getvalue(textproperty); } set { setvalue(textproperty, value); } } public static readonly dependencyproperty textproperty = dependencyproperty.register("text", typeof(string), typeof(dynamictextblock), new propertymetadata(null, new propertychangedcallback(ontextchanged))); private static void ontextchanged(dependencyobject d, dependencypropertychangedeventargs e) { ((dynamictextblock)d).ontextchanged(e); } protected virtual void ontextchanged(dependencypropertychangedeventargs e) { this.invalidatemeasure(); } #endregion #region textwrapping (dependencyproperty) /// <summary> /// gets or sets textwrapping property. corresponds textblock.textwrapping. /// </summary> public textwrapping textwrapping { { homecoming (textwrapping)getvalue(textwrappingproperty); } set { setvalue(textwrappingproperty, value); } } public static readonly dependencyproperty textwrappingproperty = dependencyproperty.register("textwrapping", typeof(textwrapping), typeof(dynamictextblock), new propertymetadata(textwrapping.nowrap, new propertychangedcallback(ontextwrappingchanged))); private static void ontextwrappingchanged(dependencyobject d, dependencypropertychangedeventargs e) { ((dynamictextblock)d).ontextwrappingchanged(e); } protected virtual void ontextwrappingchanged(dependencypropertychangedeventargs e) { this.textblock.textwrapping = (textwrapping)e.newvalue; this.invalidatemeasure(); } #endregion #region lineheight (dependencyproperty) /// <summary> /// gets or sets lineheight property. property corresponds textblock.lineheight; /// </summary> public double lineheight { { homecoming (double)getvalue(lineheightproperty); } set { setvalue(lineheightproperty, value); } } public static readonly dependencyproperty lineheightproperty = dependencyproperty.register("lineheight", typeof(double), typeof(dynamictextblock), new propertymetadata(0.0, new propertychangedcallback(onlineheightchanged))); private static void onlineheightchanged(dependencyobject d, dependencypropertychangedeventargs e) { ((dynamictextblock)d).onlineheightchanged(e); } protected virtual void onlineheightchanged(dependencypropertychangedeventargs e) { textblock.lineheight = lineheight; this.invalidatemeasure(); } #endregion #region linestackingstrategy (dependencyproperty) /// <summary> /// gets or sets linestackingstrategy dependencyproperty. corresponds textblock.linestackingstrategy. /// </summary> public linestackingstrategy linestackingstrategy { { homecoming (linestackingstrategy)getvalue(linestackingstrategyproperty); } set { setvalue(linestackingstrategyproperty, value); } } public static readonly dependencyproperty linestackingstrategyproperty = dependencyproperty.register("linestackingstrategy", typeof(linestackingstrategy), typeof(dynamictextblock), new propertymetadata(linestackingstrategy.blocklineheight, new propertychangedcallback(onlinestackingstrategychanged))); private static void onlinestackingstrategychanged(dependencyobject d, dependencypropertychangedeventargs e) { ((dynamictextblock)d).onlinestackingstrategychanged(e); } protected virtual void onlinestackingstrategychanged(dependencypropertychangedeventargs e) { this.textblock.linestackingstrategy = (linestackingstrategy)e.newvalue; this.invalidatemeasure(); } #endregion /// <summary> /// textblock gets set control's content , ultiately command /// displays our text /// </summary> private textblock textblock; /// <summary> /// initializes new instance of dynamictextblock class /// </summary> public dynamictextblock() { // create our textblock , initialize this.textblock = new textblock(); this.content = this.textblock; } /// <summary> /// handles measure part of measure , arrange layout process. during process /// measure textblock we've created content increasingly smaller amounts /// of text until find text fits. /// </summary> /// <param name="availablesize">the available size</param> /// <returns>the base of operations implementation of measure</returns> protected override size measureoverride(size availablesize) { // create code easier read bool wrapping = this.textwrapping == textwrapping.wrap; size unboundsize = wrapping ? new size(availablesize.width, double.positiveinfinity) : new size(double.positiveinfinity, availablesize.height); string reducedtext = this.text; // set text , measure see if fits without alteration this.textblock.text = reducedtext; size textsize = base.measureoverride(unboundsize); while (wrapping ? textsize.height > availablesize.height : textsize.width > availablesize.width) { int prevlength = reducedtext.length; reducedtext = this.reducetext(reducedtext); if (reducedtext.length == prevlength) { break; } this.textblock.text = reducedtext + "..."; textsize = base.measureoverride(unboundsize); } homecoming base.measureoverride(availablesize); } /// <summary> /// reduces length of text. derived classes can override utilize different techniques /// reducing text length. /// </summary> /// <param name="text">the original text</param> /// <returns>the reduced length text</returns> protected virtual string reducetext(string text) { homecoming text.substring(0, text.length - 1); } }

and in xaml utilize this:

<mynamespace:dynamictextblock grid.column="0" text="dies ist ein text der sehr lang ist und umebrochen wird" horizontalalignment="left" verticalalignment="top" margin="-1,0,0,0" foreground="{binding ishighlighted, converter={staticresource booltobrushconverter}}" fontsize="32" />

everything works cool. want apply style this. next doesn´t work:

style="{staticresource phonetextlargestyle}"

how solution must extended take staticressource style?

styles in windows phone have targettype , can set controls of type or subtypes (child classes). phonetextlargestyle's target textblock, can't set on other controls (like dynamictextblock).

you need create new style targets dynamictextblock , sets values like. guess can find out 1 way or phonetextlargestyle style does.

xaml windows-phone-8

No comments:

Post a Comment