c# - WPF - Remove a "User Control" Child from a StackPanel -


i'm trying make wpf ui user can edit query search database. query created according consumer chooses comboboxes like this , can create filters wants long clicks add new condition button.

i created comboboxes template user control :

user control xaml:

<stackpanel orientation="horizontal" >         <button                 name="deletefilter"                  horizontalalignment="left"                 margin="5"                 content="-"             click="deletefilter_onclick">         </button>         <combobox                  text="property"                 x:name="property"                 width="100"                 datacontext="{staticresource someviewmodel}"                 itemssource="{binding properties}"                 displaymemberpath="name"              selectionchanged="property_onselectionchanged"/>         <combobox              text="propertyoperator"             x:name="operator"             itemssource="{binding operators}"             displaymemberpath="name"             selectionchanged="operator_onselectionchanged">         </combobox>         <textbox                  x:name="value"                 text="value"                 textalignment="center"                 width="100"                 margin="5"/> </stackpanel> 

whenever user clicks add new condition button, call event:

private void addfilterbutton_onclick(object sender, routedeventargs e)     {         var conditionusercontrol = new conditionusercontrol();         stackpanel.children.add(conditionusercontrol);     } 

everything works correctly.

my question:

how can delete user control child clicking deletefilter button exists in user control template.

i tried this:

stackpanel.children.remove(..); 

to remove child mainwindow how know child user clicked.

try this:

private void deletefilter_onclick(object sender, routedeventargs e) {     button btn = sender button;     var conditionusercontrol = findparent<conditionusercontrol>(btn);     if (conditionusercontrol != null)     {         var sp = findparent<stackpanel>(conditionusercontrol);         if (sp != null)             sp.children.remove(conditionusercontrol);     } }   private static t findparent<t>(dependencyobject dependencyobject) t : dependencyobject {     var parent = visualtreehelper.getparent(dependencyobject);      if (parent == null) return null;      var parentt = parent t;     return parentt ?? findparent<t>(parent); } 

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -