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>
Basically, it can not find the StaticResource
because 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With