c# - Style Inheritance with UserControl -


i trying customise style of usercontrol, inheriting style , adding additional styling it. made small project try this. let's have user control called usercontrol1 (what contains irrelevant - it's empty in sample project).

i'm using in mainwindow follows:

<window x:class="wpfstyleinheritance.mainwindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         xmlns:local="clr-namespace:wpfstyleinheritance"         title="mainwindow" height="350" width="525">     <grid>         <local:usercontrol1>             <local:usercontrol1.style>                 <style targettype="{x:type local:usercontrol1}" basedon="{x:type local:usercontrol1}">                     <setter property="margin" value="0" />                 </style>             </local:usercontrol1.style>         </local:usercontrol1>     </grid> </window> 

on basedon part, i'm getting error:

the specified value cannot assigned. following type expected: "style".

if change basedon="{staticresource {x:type local:usercontrol1}}", error:

the resource "{x:type local:usercontrol1}" not resolved.

how can working , live happily ever after?

edit: posted in comments of answer here, if go staticresource route , run, xamlparseexception message:

cannot find resource named 'wpfstyleinheritance.usercontrol1'. resource names case sensitive.

additional info: if replace instances of local:usercontrol1 in mainwindow markup button, works quite nicely. problem user controls.

edit 2: if add style in usercontrol, problem remains:

<usercontrol.style>     <style targettype="usercontrol">         <setter property="height" value="0" />     </style> </usercontrol.style> 

basedon syntax incorrect. should be:

basedon="{staticresource {x:type local:usercontrol1}}" 

and if getting designer error, try re-compile code. make sure namespace added correctly mapped usercontrol.

also, make sure mainwindow , usercontrol1 resides in same assembly.

if resides in different assemblies, have specify assembly name in namespace declaration:

xmlns:local="clr-namespace:wpfstyleinheritance;assembly=assemblyname" 

update

in case haven't defined default style usercontrol1, don't need use basedon since no default style usercontrol exist. that's why getting exception.

remove basedon , work fine.


resource have defined within xaml not default style instead local style of usercontrol overridden in case define local resource in mainwindow.

if want default style usercontrol1, declare under application resources i.e. in app.xaml.

<application.resources>    <style targettype="{x:type local:usercontrol1}">       <setter property="height" value="0" />    </style> </application.resources> 

and in mainwindow.xaml, work fine:

<local:usercontrol1>     <local:usercontrol1.style>         <style targettype="{x:type local:usercontrol1}"                basedon="{staticresource {x:type local:usercontrol1}}">             <setter property="margin" value="0" />         </style>     </local:usercontrol1.style> </local:usercontrol1> 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -