views:

109

answers:

2

Is there a way in WPF to specify a width/height as a resource, so that it can be reused in several styles for e.g. margin/padding?

A: 

Yes it is and you use it as other Resources and fetch then using StaticResource see some examples here.

Filip Ekberg
Sorry Filip, I'm not seeing where a width/height is set and used as a resource in that example?
devdigital
There are examples on how you use it in other contexts, try them, read between the lines and explore.
Filip Ekberg
+2  A: 

Sure.

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:sys="clr-namespace:System;assembly=mscorlib"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
  <Page.Resources>
    <sys:Double x:Key="Height">200</sys:Double>
    <sys:Double x:Key="Width">200</sys:Double>
  </Page.Resources>
  <Grid>  
    <Rectangle 
      Height="{StaticResource Height}" 
      Width="{StaticResource Width}" 
      Fill="Blue"/>
  </Grid>
</Page>
Robert Rossney