locked
How do you use a resource as a value for the Margin property? RRS feed

  • Question

  • Hello,

    How do you use a resource as a value for the Margin property?

    I have the following resource defined:

      <Window.Resources>
        <sys:Double x:Key="AppBorderSize">30</sys:Double>
      </Window.Resources>
    

    I would like to use the "AppBorderSize" resource in the following:

      <Grid Margin="{StaticResource AppBorderSize}, 0, {StaticResource AppBorderSize}, 0">
    However, the above it not allowed.

    The following work fine:

      <RepeatButton Width="{StaticResource AppBorderSize}"
        Height="{StaticResource AppBorderSize}" />
    Regards,

    Robert

     

    Sunday, August 29, 2010 6:18 PM

Answers

  • Hi Robert,

    <sys:Double xmlns:sys="clr-namespace:System;assembly=mscorlib" 
          x:Key="MarginLeftValue">50</sys:Double>
    <Thickness x:Key="MarginResource" 
          Left="{StaticResource MarginLeftValue}" 
          Top="5" Right="5" Bottom="5" />
    ...  
    <Button Content="Test" Margin="{StaticResource MarginResource}"/>
    
    
    guess you mean something along the lines of the above then. Note that the sys-Namespace declaration can of course be set in the Window-tag as well.


    Cheers,
    Olaf
    http://blogs.intuidev.com
    • Marked as answer by Min Zhu Monday, September 6, 2010 2:30 AM
    Monday, August 30, 2010 7:15 AM
  • Hi Robert,

    The value of Margin property is a Thickness object and XAML use a TypeConverter to convert the string "30,0,30,0" to a Thickness. This TypeConverter doesn't support StaticResource syntax. If you want to use a StaticResource here, you need to set Margin to a Thickness object instead of a string since it cannot be converted by TypeConverter.

    For example you could change

        <Grid Margin="{StaticResource AppBorderSize}, 0, {StaticResource AppBorderSize}, 0">
    
    

    to

        <Grid>
          <Grid.Margin>
            <Thickness Left="{StaticResource AppBorderSize}" Top="0" Right="{StaticResource AppBorderSize}" Bottom="0"/>
          </Grid.Margin>
        </Grid>
    

    Or you can take Olaf's suggestion to define a Thickness object as a resource.

    Hope this helps.

    If you still have any doubts or concerns about this issue, please feel free to let me know.

    Best regards,

    Min


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Min Zhu Monday, September 6, 2010 2:30 AM
    Wednesday, September 1, 2010 6:09 AM

All replies

  • Hi Robert,

    Margin actually is a Thickness, so you would be using either one of ...

    <Thickness x:Key="Margin1" Left="0" Top="8" Right="-4" Bottom="0"/>
    <Thickness x:Key="Margin2">0,8,-4,0</Thickness>
    
    I've posted a bit about the above in part 1 of my TabControl tutorial - look for the "A note regarding the definition of Thickness-resources in VS2008" section.


    Cheers,
    Olaf
    http://blogs.intuidev.com
    Sunday, August 29, 2010 8:13 PM
  • Hello Olaf,

    Thanks, but that is not what I'm looking for.  I do not want the Margin to become a resource.  I want the Margin property to be able to use a resource for one or more of its arguments.

    Regards,

    Robert

    Sunday, August 29, 2010 8:22 PM
  • Hi Robert,

    <sys:Double xmlns:sys="clr-namespace:System;assembly=mscorlib" 
          x:Key="MarginLeftValue">50</sys:Double>
    <Thickness x:Key="MarginResource" 
          Left="{StaticResource MarginLeftValue}" 
          Top="5" Right="5" Bottom="5" />
    ...  
    <Button Content="Test" Margin="{StaticResource MarginResource}"/>
    
    
    guess you mean something along the lines of the above then. Note that the sys-Namespace declaration can of course be set in the Window-tag as well.


    Cheers,
    Olaf
    http://blogs.intuidev.com
    • Marked as answer by Min Zhu Monday, September 6, 2010 2:30 AM
    Monday, August 30, 2010 7:15 AM
  • Hi Robert,

    The value of Margin property is a Thickness object and XAML use a TypeConverter to convert the string "30,0,30,0" to a Thickness. This TypeConverter doesn't support StaticResource syntax. If you want to use a StaticResource here, you need to set Margin to a Thickness object instead of a string since it cannot be converted by TypeConverter.

    For example you could change

        <Grid Margin="{StaticResource AppBorderSize}, 0, {StaticResource AppBorderSize}, 0">
    
    

    to

        <Grid>
          <Grid.Margin>
            <Thickness Left="{StaticResource AppBorderSize}" Top="0" Right="{StaticResource AppBorderSize}" Bottom="0"/>
          </Grid.Margin>
        </Grid>
    

    Or you can take Olaf's suggestion to define a Thickness object as a resource.

    Hope this helps.

    If you still have any doubts or concerns about this issue, please feel free to let me know.

    Best regards,

    Min


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Min Zhu Monday, September 6, 2010 2:30 AM
    Wednesday, September 1, 2010 6:09 AM