views:

23

answers:

1

Has anyone solved this: displaying graphical resources within a button

create a resource, for instance a rectangle

<UserControl.Resources>
    <Rectangle x:Key="myRectangle" Fill="Red" Height="100" Width="100"/>
</UserControl.Resources>

then set the content of the button to the resource

<Button Content="{StaticResource myRectangle}"/>

when you build inside blend 4 RC you get the error "Value does not fall within the expected range." Visual studio does not show this error. When you run the site the button doesn't show any content. This technique works in WPF without problems.

Anyone got any ideas?

A: 

This can be done by directly setting the shape as the button's content. For eg:

<Button Height="120" Width="120">
        <Rectangle Fill="Red" Height="100" Width="100"/>
</Button>

FrameworkElement.Resources are generally used for storing nonvisual elements, brushes, etc. For your case (I think) you'll need to store your xaml as a data template, again not sure if this works with Buttons, it is used for things like ListBoxes. See here: resources description on msdn. The link further contains pointers to information for Data Templates etc.

Anuj Seth
cheers for the response.I was looking to keep bulky vector imports from illustrator in a separate resource file so that I can keep the main structure files clean.I know that I can use bitmaps but I prefer to use vectors where I can.I'm coming from WPF and the above technique works fine
devsigner