c# - User Control Child elements sharing Value across multiple instances -


i have made user control, fontselector, groups combobox fontfamily selection , 3 togglebuttons bold, italics, underline options. having issue combobox's selecteditem property affecting instances of user control within same window. example, changing combobox selection on one, automatically change other. clarity. don't want behavior. surprised user control implicitly affecting user control.

xaml

<grid x:name="grid" background="white" datacontext="{binding relativesource={relativesource ancestortype=local:fontselector}}">             <combobox x:name="combobox" width="135"               selecteditem="{binding path=selectedfontfamily}" style="{staticresource fontchoosercomboboxstyle}"                               itemssource="{binding source={staticresource systemfontfamilies}}"/> </grid> 

code behind

the clr property combobox's selecteditem bound to. code shown here in user control code behind file, not viewmodel.

        private fontfamily _selectedfontfamily;     public fontfamily selectedfontfamily     {                 {             return _selectedfontfamily;         }         set         {             if (_selectedfontfamily != value)             {                 _selectedfontfamily = value;                  // modify external dependency property value.                 if (value != selectedfont.fontfamily)                 {                     selectedfont = new typeface(value, getstyle(), getweight(), fontstretches.normal);                 }                  // notify.                 raisepropertychanged(nameof(selectedfontfamily));             }         }     } 

the dependency property updates it's value based on value of combobox's selecteditem property. packages fontfamily value typeface object.

public typeface selectedfont     {         { return (typeface)getvalue(selectedfontproperty); }         set { setvalue(selectedfontproperty, value); }     }      // using dependencyproperty backing store selectedfont.  enables animation, styling, binding, etc...     public static readonly dependencyproperty selectedfontproperty =         dependencyproperty.register("selectedfont", typeof(typeface), typeof(fontselector),             new frameworkpropertymetadata(new typeface("arial"), frameworkpropertymetadataoptions.bindstwowaybydefault,                 new propertychangedcallback(onselectedfontpropertychanged)));      private static void onselectedfontpropertychanged(dependencyobject d, dependencypropertychangedeventargs e)     {         var instance = d fontselector;         var newfont = e.newvalue typeface;          if (newfont != null)         {             instance.selectedfontfamily = newfont.fontfamily;          }     } 

edit

i think may have figured out going on. can replicate binding itemssource following collection view source.

<collectionviewsource  x:key="systemfontfamilies" source="{binding source={x:static fonts.systemfontfamilies}}">         <collectionviewsource.sortdescriptions>              <scm:sortdescription propertyname="source"/>         </collectionviewsource.sortdescriptions>     </collectionviewsource> 

you can replicate behavior placing 2 comboboxes , binding both of them collectionviewsource. now, seemingly implicitly track each others selecteditem. without data binding outside of itemssource. seem collectionviewsource somehow playing part in selecteditem is.

i'd make bit different. i'll introduce solution using string, not fontfamily or fontweight, since have no vs here right now. (in order have working, please change list of fontfamilies list of strings bind them.)

your selector usercontrol:
- xaml ok (but won't need x:name)
- codebehind of usercontrol (later: uc) should change, solve binding. should have dependencyproperty, lets' call selectedfontfamily, represent selected string combobox:

public string selectedfontfamily {     { return (string)getvalue(selectedfontfamilyproperty); }     set { setvalue(selectedfontfamilyproperty, value); } }  public static readonly dependencyproperty selectedfontfamilyproperty = dependencyproperty.register("selectedfontfamily", typeof(string), typeof(youruc), new propertymetadata(string.empty)); 

the window, contains uc:
- should include namespace of uc's folder in opening tag of window, eg:

<window  ... xmlns:view="clr-namespace:yourprojectname.views.usercontrols"> 

- window's datacontext should have property public set option (feel free implement inotifypropertychange on it):

public string fontfamily {get; set;} 

- in window's xaml use uc way:

<view:youruc selectedfontfamily="{binding fontfamily, mode=twoway, updatesourcetrigger=propertychanged}"/> 

it's two-way binding. you'll find selected string value of fontfamily property every time change selecteditem.

edit: need view model class window using usercontrol. create it, make implement inotifypropertychanged interface, , set datacontext consumer window. wpf not wf, can find more if google "wpf mvvm" or that.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -