views:

481

answers:

2

I have a style for a textblock that is set inside my app.xaml this is then applied to textblocked through out my application and works fine.

However i get an error: "could not create instance of type" if i apply this style to a textblock within my user control, how come this is a problem?

<UserControl x:Class="Client.Usercontrols.MyButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" MinHeight="30" MinWidth="40"
DataContext="{Binding RelativeSource={RelativeSource Self}}">

<Button Width="Auto" HorizontalAlignment="Center">

    <Border CornerRadius="5" BorderThickness="1" BorderBrush="Transparent" >
        <Grid>
            <Image Name="tehImage" Source="{Binding ImageSource}" />
            <TextBlock Name="tehText" Text="{Binding Text}" 
                Style="{StaticResource ButtonText}" /> <-- This causes error
        </Grid>
    </Border>

</Button>

Thanks, Kohan

-- App.Xaml Code --

<Application x:Class="Client.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Mainpage.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Styles/CascadingStyles.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

-- CascadingStyles.Xaml --

 <Style TargetType="{x:Type TextBlock}" x:Key="ButtonText" >
    <Setter Property="FontSize" Value="10" />
    <Setter Property="VerticalAlignment" Value="Bottom" />
    <Setter Property="HorizontalAlignment" Value="Center" />
    <Setter Property="FontFamily" Value="Lucida Sans Unicode" />
    <Setter Property="Foreground" Value="#0F004E" />
 </Style>
+1  A: 

basically, it can not find the StaticResource bc it is not in the file with your user control. UserControl.xaml knows nothing about App.xaml. You should use DynamicResource instead, this way it will be applied at runtime.

Muad'Dib
I changed to DynamicResource as suggested and it works, it also works if i set the style in <UserControl.Resources></..>Just out of interest, how come App.Config's scope does not reach a UserControl but it does reach all controls in any page or window?
Kohan
basically, you have to think of a UserControl as a stand-alone entity. It could, for example, live in a different assembly. A Custom Control works the same way. They only know what you tell them. Ideally, you would put your styles in a ResourceDicationary and include them where you need them--the App.xaml, your user control, etc. This makes it easier to "skin" or "theme" your stuff--just swap out resource dictionaries.
Muad'Dib
Ahh, okay. Makes sense. Thanks
Kohan
A: 

The previous answer is absolutely incorrect. You can definitely define resources at the application level and reference them from within UserControls. In fact, that can often increase performance to prevent resource duplication. Application resources are checked 3rd in the list for Static Resources as described on this page under the heading "Static resource lookup behavior".

I'm guessing you have a typo or some other problem causing your error. Could you post the app.xaml code?

Bryce Kahle
Added code as requested, seems odd that it was causing the error as a normal resource and then works if i assign it in a local resource or make it dynamic.
Kohan
Copied code verbatim and builds/runs with no errors. Do you have more details about the error you were receiving?
Bryce Kahle
Note: I did remove the bindings in the UserControl since I didn't want to spend the time to implement the properties. Maybe that is the source of the problem?
Bryce Kahle