How do I bind to uninstantiated resources ?
Hi,
How do I bind a StackPanel's property to a property of an object that's declared within <StackPanel.Resources> ?
For example the following code throws an exception at runtime:
<StackPanel MinHeight="{Binding Source={StaticResource HEIGHT_of_item1}}" MinWidth="{Binding Source={StaticResource WIDTH_of_item1}}">
<StackPanel.Resources>
<ObjectDataProvider ObjectType="{x:Type local:getBitmapSize}" x:Key="bitmap1">
</ObjectDataProvider>
<ObjectDataProvider ObjectInstance="{StaticResource bitmap1}" MethodName="getwidth" x:Key="WIDTH_of_item1">
<ObjectDataProvider.MethodParameters>
<system:String>pack://application:,,/Imagenes/Item1.bmp</system:String>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<ObjectDataProvider ObjectInstance="{StaticResource bitmap1}" MethodName="getheight" x:Key="HEIGHT_of_item1">
<ObjectDataProvider.MethodParameters>
<system:String>pack://application:,,/Imagenes/Item1.bmp</system:String>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</StackPanel.Resources>
(...)
</StackPanel>
Now, the following code (which is the same as above except that MinHeight and MinWidht are hard-coded) works:
<StackPanel MinHeight="6.10cm" MinWidth="8.92cm">
<StackPanel.Resources>
<ObjectDataProvider ObjectType="{x:Type local:getBitmapSize}" x:Key="bitmap1">
</ObjectDataProvider>
<ObjectDataProvider ObjectInstance="{StaticResource bitmap1}" MethodName="getwidth" x:Key="WIDTH_of_item1">
<ObjectDataProvider.MethodParameters>
<system:String>pack://application:,,/Imagenes/Item1.bmp</system:String>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<ObjectDataProvider ObjectInstance="{StaticResource bitmap1}" MethodName="getheight" x:Key="HEIGHT_of_item1">
<ObjectDataProvider.MethodParameters>
<system:String>pack://application:,,/Imagenes/Item1.bmp</system:String>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</StackPanel.Resources>
<TextBlock Text="{Binding Source={StaticResource WIDTH_of_item1}}" />
<TextBlock Text="{Binding Source={StaticResource HEIGHT_of_item1}}" />
</StackPanel>
The two TextBlocks prove that the getBitmapSize class in the codebehind gets instantiated, and the two methods 'getwidth' and 'getheight' do their job.
Regarding the runtime exception, it occurs to me that by the time StackPanel gets instantiated the <StackPanel.Resources> don't exist yet and hence the error.
Is there a workaround for this ?
Thank you,
Fernando Ronci

