user589195
user589195

Reputation: 4240

Style not being picked up WPF

I am trying to set up styles in an external DLL that will be used to define how certain controls should look.

I have a resource dictionary defined in an external DLL that has a style targeted at TextBoxes:

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="{x:Type TextBox}" x:Key="TextStyle">
        <Setter Property="Text" Value="Moo"/>
    </Style>
</ResourceDictionary>

I then reference this built DLL in another application. This works:

<Window x:Class="HTMLTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/GX3Resources;component/Resources.xaml"/>

            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

    <Grid>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="45,217,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Style="{StaticResource TextStyle}"/>
    </Grid>
</Window>

This does not:

<Window x:Class="HTMLTest.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="pack://application:,,,/GX3Resources;component/Resources.xaml"/>

                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Window.Resources>

        <Grid>
            <TextBox Height="23" HorizontalAlignment="Left" Margin="45,217,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
        </Grid>
    </Window>

I would hope the above would pick up the TextStyle as is it a text box and the style is targetted at textboxes.

Upvotes: 1

Views: 455

Answers (1)

Botz3000
Botz3000

Reputation: 39600

If you can edit the original style, you can use it for all Textboxes automatically by setting its key property to the target type:

<Style TargetType="{x:Type TextBox}" x:Key="{x:Type TextBox}">

If you can't change the style, try to create another one based on it:

<Style TargetType="{x:Type TextBox}" 
       BasedOn="{StaticResource TextStyle}" 
       x:Key="{x:Type TextBox}">
</Style>

Upvotes: 2

Related Questions