Visual Studio Development Bookmark and Share   
 index > Visual Studio WPF Designer > When binding Style on a UserControl to Application.Resource a consumer of the usercontrol fails to render in Visual Studio.
 

When binding Style on a UserControl to Application.Resource a consumer of the usercontrol fails to render in Visual Studio.

Hi,

I have a WPF application that has a UserControl called 'Address' which sets up a simple form with a grid some labels and some data input controls (TextBoxes..etc.)

I made a little style resource to add a gradient to a grid row background for my headings like this:
<Application.Resources>
        <Style x:Key="GridHeaderFade" TargetType="{x:Type Grid}">
            <Setter Property="Grid.Background">
                <Setter.Value>
                    <LinearGradientBrush ColorInterpolationMode="SRgbLinearInterpolation" >
                        <GradientStop Color="#FFFFFFFF" Offset=".3"/>
                        <GradientStop Color="#B0a0a0a0" Offset=".85"/>
                        <GradientStop Color="#FFFFFFFF" Offset=".99"/>
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
        </Style>
    </Application.Resources>
Then in my address control I use:
<Grid Grid.Row="0" Style="{StaticResource GridHeaderFade}" >
            <Label HorizontalAlignment="Left" Margin="0,-7,0,0"  FontSize="16" FontWeight="Bold"  Name="lblAddressName" Content="{Binding AddressName}" />
</Grid>
While I am editing the Address.xaml UserControl the gradient shows in my row 0. The solution complies no problem. In fact it works when you run the WPF App.

Now, I have another UserControl named 'Contact' this user control consumes the Address control twice. One is named the Billing Address and one is named the Mailing Address. This control is setup like this:
<UserControl x:Class="Administrator.Controls.Contact"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="clr-namespace:Administrator.Controls"
    Height="500" Width="600">
    <Grid Height="800" Width="600">
        
        <ScrollViewer Margin="0,0,0,67" Name="scrollViewer1">
            <StackPanel>
                <controls:Address x:Name="addrBilling" HorizontalAlignment="Left" AddressName="Billing Address" Margin="0,0,0,0" Width="581"></controls:Address>
                <controls:Address x:Name="addrHome" HorizontalAlignment="Left" AddressName="Home Address" Margin="0,0,0,0" Width="581"></controls:Address>
            </StackPanel>
        </ScrollViewer>
    </Grid>
</UserControl>
Note: My Address.xaml and Contact.Xaml are in the same WPF Appliction project in a folder named 'Controls' and thus the namespace of the controls are 'Administrator.Controls', explaining the xmlns at the top of the code.

So when I add these Address controls to the Contact User Control, the designer complains that there is a problem with the XAML. The blue squigly XAML error is 'Could not create instance of 'Address''. The project works it simply will not let me design the Contact.xaml.

If I delete theStyle="{StaticResource GridHeaderFade}" from the Row="0" line of my Address.xaml file the designer will show both Address controls correctly on the designer but of course no fade in my grid row.

What I am trying to accomplish:
I would like to be able to 'skin' or 'style' my controls at a high level and allow all my sub-controls in the project to reuse the same style elements. This is why I defined them in the Application.Resources section. If there is a better way for this please explain.

Thanks for your help,
David Sandor



Mark the best replies as answers!
dsandor  Sunday, July 05, 2009 5:54 PM
David,

This is a know issue with Visual Studio 2008 that has been corrected in Visual Studio 2010. When the next Beta of Visual Studio 2010 is released please try your scenario and let us know if this does not solve your problem.

Cheers, Karl - Microsoft Product Team, Code Project MVP - http://karlshifflett.wordpress.com/
karl140.6  Wednesday, August 26, 2009 4:30 PM

You can use google to search for other answers

Custom Search

More Threads

• PlacementAdapter and ParentAdapter
• Definition of PrimarySelectionAdornerProvider
• Default XML Namespace for Custom Control
• Adorner cannot show upon WindowsFormsHost
• How to change the appearance of the controls using Expression design together with Expression Blend?
• XAML intellisense problem
• Cider support for DataTemplates
• Strange Error when creating WPF App in VS2008 Express
• User Tasks in a WPF document?
• Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding