Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7653875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:08:08+00:00 2026-05-31T12:08:08+00:00

I’ve looked but apparently can’t get the syntax correct when correlating to classes and

  • 0

I’ve looked but apparently can’t get the syntax correct when correlating to classes and styles. I have controls that have certain behavior. Some I derive to add additional behavior. Now, I want a style to correspond to each version. In its simplest example, I will ignore those classes as I know style is specifically associated to visual and not function impact. Some previous questions were close, but I am still missing something.

public class MyLabel : Label
{}

public class MyLabel2 : MyLabel
{}

So from the above, I should be able to have 3 styles in my resource dictionary. One for the generic “Label”, one for instances where I have a control that is derived from “MyLabel”, and another for “MyLabel2”. The “local:” points to my class library per the “Theme” xaml declaration.

<Style x:Key="baseLabel" TargetType="Label">
   <Setter Property="FontSize" Value="10" />
</Style>

<Style x:Key="styleMyLabel" TargetType="local:MyLabel"  BasedOn="{StaticResource baseLabel}" >
   <Setter Property="FontSize" Value="14" />
</Style>

<Style x:Key="styleMyLabel2" TargetType="{x:Type local:MyLabel2}" BasedOn="{StaticResource styleMyLabel}" >
   <Setter Property="FontSize" Value="22" />
</Style>

Again, for very simplistic purposes, just using a simple label for inheritance following purposes. In my window xaml file. Per the window xaml, the “src:” points to my class library, which is the same library as “local:” referenced in the theme (but I tried both ways… to have both instances refer to the xmlns:local in both, but it didn’t appear to make a difference).

<src:MyLabel Content="Does Not Respect Style even though derived from MyLabel class" />
<src:MyLabel Content="This one works " Style="{DynamicResource styleMyLabel}" />

<src:MyLabel2 Content="This one doesnt work"/>
<src:MyLabel2 Content="This one works " Style="{DynamicResource styleMyLabel2}" />

I’m thinking I shouldn’t have to explicitly identify the style if its derived from a class that has a style of the corresponding Target Type. Hopefully its something stupid simple but I can’t get my head wrapped around it…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:08:09+00:00Added an answer on May 31, 2026 at 12:08 pm

    Ok, I’ve stumbled into what APPEARS to be working as the answer, but don’t understand why… If I try to change the style for the MyLabel2 (which is the second derived instance) from

    <Style x:Key="styleMyLabel2" TargetType="{x:Type local:MyLabel2}" BasedOn="{StaticResource styleMyLabel}" >
       <Setter Property="FontSize" Value="22" />
    </Style>
    

    to (just removing the x:Key element)

    <Style TargetType="{x:Type local:MyLabel2}" BasedOn="{StaticResource styleMyLabel}" >
       <Setter Property="FontSize" Value="22" />
    </Style>
    

    the XAML works… WITHOUT having to explicitly associate the “Style” to be used. It finds it based on the class association directly.

    <src:MyLabel2 Content="Now Works as expected"/>
    

    Now that it works, can someone maybe explain why the extra use of the x:Key reference kills it? ie: if you have the x:key, its not implied by the class instance, but not having it, the direct class DOES find it automatically.

    EXPANDED FROM MY PLAYING AROUND WITH STYLES…

    So, here is some extras that I’ve found out. A style can be defined more than once to the same TARGET TYPE… However, one can have an x:key reference, and another a BasedOn reference. This APPEARS to be like overloading a function with same number, but different data type parameters.

    In my problem, as soon as the “x:Key” reference was added to the style, any direct class instances of the “TargetType” were NOT automatically linked up to the proper style, almost like the style declaration with the “x:key” REQUIRES any instances of a class to explicitly add the style context within the xaml of the control. So now, how to have it BOTH ways. I created a SECOND style with the TargetType as the baseclass, but had ITs BasedOn pointing to the x:key referenced one. This way, I get the default customization of the style, yet can STILL assign it to the baseclass and the derived class as well, so all 3 versions of a control can be automatically synchronized with the corresponding Style without EXPLICIT referencing.

    <Style TargetType="Label" x:Key="wpfBaseLabel">
       <Setter Property="FontSize" Value="11" />
    </Style>
    
    <Style TargetType="Label" BasedOn="{StaticResource wpfBaseLabel}" />
    
    <Style TargetType="{x:Type local:MyLabel}" BasedOn="{StaticResource wpfBaseLabel}">
       <Setter Property="Foreground" Value="Blue" />
    </Style>
    

    So, now, within the final XAML, I can have all 3 “classes” used with or without explicit style association.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.