c# - Inherting and Customizing WPF controls -
we developing piece of software intended work both stand alone plugin different cad programs. note meant discrete versions of software. not 1 monolith version functions plugin stand alone version. developing interface using wpf. depending on context (i.e. whether stand alone version or hooked cad program) want able change , importantly behaviour of interface. rather not have rewrite entire interface.
i give brief overview of failed idea hope provide additional context want achieve. initial idea create project defines default interface creating necessary controls , assembling them interface in page or window. if want customize interface use specific cad program inherit controls , pages , override parts want customize given context. found inherit wpf controls not straight forward.
a quick mock of setup tried outlined here.
the default view contains basic interface called defaultui collected in page. contains custom canvas(called defaultcanvas) type wrapped in scrollviewer. xaml code defaultui is.
<page x:class="defaultview.defaultui" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:defaultview" mc:ignorable="d" d:designheight="300" d:designwidth="300" title="defaultui"> <grid> <scrollviewer horizontalscrollbarvisibility="auto"> <local:defaultcanvas x:name="mcanvas" horizontalalignment="stretch" verticalalignment="stretch" background="transparent" allowdrop="true" ishittestvisible="true"> </local:defaultcanvas> </scrollviewer> </grid> </page>
what tried in cadview , standalone project create derived classes cadviewcanvas , cadviewui defaultcanvas , defaultui , able customize behaviour of these. want customize behaviour of canvas, cadviewui needs use derived class rather defaultcanvas. reason want derive defaultui might contain several controls , want customize one. here realized might have wrong idea assume mcanvas initialized instance of defaultcanvas when cadviewui calls it's base constructor.
the additional functionality needed in cadviewcanvas logic connects thecad software in question. in cadview set of wrappers included. each wrapper connects object our model object in cad software. cadviewcanvas should work wrapper, instead of directly on model object. same action taken model object, logic needed keep cad object date well.
inheritance of controls contain child controls can hard when want adjust child control in derived classes.
i advice have controls in base class can customize through styles. applying different style child control in derived class 'easy' , clean: define new style (that can based on default style)
when need different behavior of controls need provide behavior in way:
- put possible behaviors in base class , add properties turn them on , off these can set in style. requires know upfront needed , create bloated controls. recommend against this.
- or implement different controls. means cannot add these base control , have add them derived controls. cleaner in opinion.
in short: functionality not same derived classes not belong in base class not put there. if simple, visual things layout/colors/visibility style might proper solution.
Comments
Post a Comment