Can XAML “deduce” inheritance from C#?

2021-01-15T20:57:59.483+00:00

I wonder if XAML is somehow able to "deduce" the C# inheritance. Let me explain:

I have these two C# classes:

public class AStyleSelector: StyleSelector {
public Style aStyle { get; set; }

public override Style SelectStyle(object item, DependencyObject container)
{
    //....
}

}

public class BStyleSelector : AStyleSelector{
public Style bStyle { get; set; }

public override Style SelectStyle(object item, DependencyObject container)
{
    var tmp = aStyle; // this is null
    //....
}

}
BStyleSelector inherits from AStyleSelector which inherits from StyleSelector. They both implement SelectStyle.

Then I have the next in a XAML file: Here I defined two styles:

<Style // to be used in both AStyleSelector and BStyleSelector
x:Key="StyleA"
//... some styles
</Style>

<Style // to be used only in BStyleSelector
x:Key="StyleB"
//... some styles
</Style>
Here I am binding to C# classes and pointing to the defined Styles:

<AStyleSelector //pointing to the above C# class
aStyle="{StaticResource StyleA}" />

<BStyleSelector //pointing to the above C# class"
bStyle="{StaticResource StyleB}" />
Now, the problem is, in the method SelectStyle from BStyleSelector, I cannot access the value of aStyle. It is returning null.

I know there is a quick fix (that works correctly) which is this:

<BStyleSelector
aStyle="{StaticResource StyleA}" // adding this line
bStyle="{StaticResource StyleB}" />
But I don't want to reference twice the StyleA. I was expecting that XAML would deduce it's value because BStyleSelector inherits it from AStyleSelector.

Is there some way to do this?

Thanks a lot in advance.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,783 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,997 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
814 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. DaisyTian-1203 11,626 Reputation points
    2021-01-18T06:58:37.927+00:00

    You can make the StyleB base on StyleA as below:

      <Style x:key="StyleB" BasedOn="{StaticResource StyleA}" TargetType="BStyleSelector">  
            <!-- Some other Style -->  
       </Style>    
    

    Then use it like

    <BStyleSelector bStyle="{StaticResource StyleB}" />  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.