Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How a derived class of FrameworkElement creates visual children OR renders #646

Open
verelpode opened this issue Oct 8, 2018 · 5 comments
Assignees
Labels
area-xaml XAML-related feedback. doc-bug Topic has missing, outdated, unclear, confusing info, or has broken functionality. eng-triaged Issue has been triaged by engineering team responsible. Pri2 Topic is in the second 25% of page views for the repo. windows/prod Topic product is Windows-related.

Comments

@verelpode
Copy link

verelpode commented Oct 8, 2018

Thanks for documenting FrameworkElement. Could you please explain how a derived class of FrameworkElement (or UIElement) can create visual child elements and/or render itself? How to create visual child elements without using a control template? I do realize that the documentation says "Typically, you don't derive classes directly from FrameworkElement", but occasionally it is necessary to derive directly from FrameworkElement, thus it would be greatly appreciated if you could explain how to do this, in particular the rendering or visual children part.

I understand that one option is to derive from Control instead of FrameworkElement and then use a control template, but sometimes templates are not the right solution for a particular UI element class, therefore how can we derive directly from FrameworkElement?

For comparison, in the older WPF-Desktop, a derived class could create visual child elements by overriding the following members, but these members do not exist in UWP, therefore how is it done in UWP?

  • System.Windows.FrameworkElement.VisualChildrenCount
  • System.Windows.FrameworkElement.GetVisualChild(int)

Also in WPF-Desktop, a class could render itself by overriding System.Windows.UIElement.OnRender(System.Windows.Media.DrawingContext) but this method does not exist in UWP, therefore how is it done in UWP?

Thank you!


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

@jevansaks jevansaks added area-xaml XAML-related feedback. eng-triaged Issue has been triaged by engineering team responsible. labels Apr 8, 2019
@PRMerger8 PRMerger8 added the windows/prod Topic product is Windows-related. label Jan 9, 2020
@PRMerger8 PRMerger8 added the Pri2 Topic is in the second 25% of page views for the repo. label Jun 19, 2020
@hkhmsft hkhmsft added the doc-bug Topic has missing, outdated, unclear, confusing info, or has broken functionality. label Jan 25, 2021
@michael-hawker
Copy link
Contributor

@MikeHillberg @codendone running into this for something I'm trying to work on (CommunityToolkit/WindowsCommunityToolkit#4104). Can you point us in the right direction?

It seems in the WinUI repo there's a Children() directive which helps with this, but not sure how that's applicable in C# as I don't see it?

    [ContentProperty(Name = nameof(Child))]
    public class ConstrainedPresenter : FrameworkElement

image

Examples from WinUI 2 repo:

It also seems like for ParallaxView the Child property is also a DependencyProperty but this isn't case for Border and Viewbox so it it just to make it easier or do those other types just not respond to hot-swapping their children the same way?

(I'm also conflicted on naming it Constrainedbox to align with Viewbox or ConstainedBox for better casing/readibility...)

@codendone
Copy link
Contributor

@michael-hawker These controls derive from Panel (see DeriveFromPanelHelper_base as part of their definition) in order to have Panel's Children property.

@michael-hawker
Copy link
Contributor

@codendone then why in the IDL and docs does it show it inheriting from FrameworkElement directly?

https://github.com/microsoft/microsoft-ui-xaml/blob/main/dev/ParallaxView/ParallaxView.idl#L17


image

As the Children property isn't exposed publicly...

image

Border itself doesn't inherit from a Panel right?

I guess this comment explains it a bit about hiding the implementation detail? Is this possible to achieve in C#?

If we want just a single child is there a more direct way?

@codendone
Copy link
Contributor

@michael-hawker The DeriveFromPanelHelper_base makes its implementation a subclass of Panel so that it has access to the Children property. As you noted, the idl file specifies FrameworkElement as the base, and it is the idl which generates the winmd and therefore the interface that app code sees. I don't know if there is a way to achieve this in C#.

@michael-hawker
Copy link
Contributor

Filed issue microsoft/microsoft-ui-xaml#5530 for tracking exposing this in the Framework for WinUI 3. Sounds like for now I should stick with ContentPresenter or switch to Panel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-xaml XAML-related feedback. doc-bug Topic has missing, outdated, unclear, confusing info, or has broken functionality. eng-triaged Issue has been triaged by engineering team responsible. Pri2 Topic is in the second 25% of page views for the repo. windows/prod Topic product is Windows-related.
Projects
None yet
Development

No branches or pull requests

8 participants