c# - Refactor XAML in WPF -


my xaml has got long , hard maintain. wondering if there way refactoring?

here simple example:

<window x:class="refactorxaml.mainwindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         title="mainwindow" height="350" width="525">     <grid>         <stackpanel>             <button>new</button>             <button>open</button>         </stackpanel>     </grid> </window> 

how can refactor stackpanel section , write this?

<window x:class="refactorxaml.mainwindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         title="mainwindow" height="350" width="525">     <grid>         // refactored markup     </grid> </window> 

you put buttons in user control, make this:

<window …         xmlns:my="clr-namespace:somenamespace">     <grid>         <my:newandopenbuttons />     </grid> </window> 

you replace grid items control includes buttons, way impose many restrictions on other content, wouldn’t that.

so above, user control this:

<usercontrol x:class="somenamespace.newandopenbuttons" …>     <stackpanel>         <button>new</button>         <button>open</button>     </stackpanel> </usercontrol> 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -