c# - Doubts with Attach property -


well, title not descriptive because not how name i'm looking for. i'll try explain best can.

in .xaml file, control (suppose textbox), if type "text", can not used again property. if write hand, compiler displays error. explained why think it's easier.

suppose have 2 (dependencyproperty.registerattached ... ...) name "propiedad_1" , "propiedad_2" possible, if i'm using one, other can not used in same control , vice versa?

2) question, within dependency property of type string, possible check if string changed @ point (around trying not use variable , comparing), need avoid spending texbox , avoid textbox.textchanged event.

thanks!

edit

this have now.

public static readonly dependencyproperty filtersourceproperty =     dependencyproperty.registerattached("filtersource", typeof (textbox), typeof (listviewextension),         new frameworkpropertymetadata(null, ontextboxtextchanged));  public static textbox getfiltersource(dependencyobject dobj) {     return (textbox) dobj.getvalue(filtersourceproperty); }  public static void setfiltersource(dependencyobject dobj, textbox value) {     dobj.setvalue(filtersourceproperty, value); }  private static void ontextboxtextchanged(dependencyobject dobj, dependencypropertychangedeventargs e) {     var listview = dobj listview;     var textbox = e.newvalue textbox;      if ((listview == null) || (textbox == null)) return;      textbox.textchanged += delegate(object sender, textchangedeventargs tcea)     {          var view = collectionviewsource.getdefaultview(listview.itemssource);         if (view == null) return;         view.filter += item =>         {             ...             ...             ...             ...         };     }; } 

in .xaml

    <textbox name="txtfilter"              verticalalignment="center"              verticalcontentalignment="center"              text="{binding filter, mode=twoway, updatesourcetrigger=propertychanged}" charactercasing="upper"/>  <listview margin="0,5,0,0"           itemssource="{binding articles}"           issynchronizedwithcurrentitem="true"           selecteditem="{binding selectedarticle}"           tools:listviewextension.filtersource="{binding elementname=txtfilter}"> 

in viewmodel

public string filter {     { return _filter; }     set     {         if (_filter == value) return;          _filter = value;         raisepropertychanged();     } } 

what need change use of "textbox" string.

tools:listviewextension.filtersource="{binding filter}" 

your question hard understand, due lack of a good, minimal, complete code example poor english. latter understandable (though must still can ensure you're communicating possible), former should addressed.

lacking improvements, guess questions (both…for future reference, please not post 2 different questions in same post) amount following:

  1. i have 2 attached properties, make mutually exclusive in xaml editor. possible?

no. behavior you're seeing applies single property. editor complain if try set same property more once. it's easy that, since has check whether property used in element.

for 2 different properties supposed mutually exclusive, there's not feasible way modify editor or compiler's behavior check this.

as alternative, consider implementing 2 mutually-exclusive values single property, property can accept 2 different subclasses of given type, , subclasses each represent 1 of 2 mutually exclusive property types.

  1. can optimize property updates, if new value assigned same current value, no "property changed" event raised?

whether possible depends on how code written. in wpf, binding supported through use of dependencyproperty or inotifypropertychanged. neither of these imply textchanged event in object text property, stated event don't want raised.

note in general, dependencyobject.setvalue() suppress change notifications if effective value (after coercion) has not changed. note in other cases, change notifications not real performance issue.

lacking code example, not more advice on second question can offered.

if feel these answers don't reasonably or usefully address questions, please improve post more understandable.


edit:

with respect first question, , based on code snippet you've provided (not complete), simplest approach make filtersource have type object instead of textbox, , in ontextboxtextchanged(), check type of new value , handle appropriately. i.e. if it's textbox, you're doing (mostly…see (*) below), , if it's string instance, configure view's filter directly instead of putting configuration event handler.


(*) note:

i see @ least 2 areas of improvement in ontextboxtextchanged() method:

  1. there no need rebuild filter event handler because text's changed. instead, can call refresh() on view. in approach, implement event handler filter event retrieve textbox.text property value filtering. subscribe event once, , event handler textchanged call refresh().

    in string scenario, you'd use filter event handler filters using string value, no need handle (non-existent, of course) textchanged event.

  2. the bigger issue ever subscribe textchanged event. if ever change filtersource property once, you'll never notice problem, is problem. if property value ever changed again, should unsubscribing old event handler before subscribing new one. if make change describe above, textchanged event handler calling refresh(), impact of bug reduced. it's still bug.

end of note.


far second part of question goes, don't see problem needs solving. it's not clear whether you're concerned textbox.text property or filtersource property, think neither property should generate change notifications if newly set property value same old.

if think differently, please provide better code example (both minimal , complete) illustrates actual problem occurs, along clear, precise explanation of problem is: how code behaves, , how that's different how want to.


taking of above account, think ontextboxtextchanged() method should more this:

private static void ontextboxtextchanged(dependencyobject dobj, dependencypropertychangedeventargs e) {     listview listview = dobj listview;      if (listview == null) return;      var view = collectionviewsource.getdefaultview(listview.itemssource);     if (view == null) return;      if (e.newvalue textbox)     {         textbox newvalue = (textbox)e.newvalue;          view.filter += item =>         {             string filterstring = newvalue.text;              // filter based on filterstring, etc.         };          textbox.textchanged += delegate(object sender, textchangedeventargs tcea)         {             view.refresh();         };     }     else if (e.newvalue string)     {         string filterstring = (string)e.newvalue;          view.filter += item =>         {             // filter based on filterstring, etc.         };     }     else return; } 

for work, you'll of course have change type of attached property textbox object.

in above, did not bother address issue of unsubscribing textchanged or filter events. if desire fix particular problem, relatively simple: need unsubscribe old handlers events (for textchanged, if e.oldvalue textbox true of course).

of course, you'll need store old event handler delegate instances on per-listview object basis, e.g. in dictionary or maybe having private attached property (similar filtersource property, not visible other code). way, can retrieve delegate instances later unsubscribe them events.


Comments