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

  • SEARCH
  • Home
  • 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 7093981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:30:25+00:00 2026-05-28T08:30:25+00:00

Occasionally I’ll have some WPF C# code that I’d like to write in a

  • 0

Occasionally I’ll have some WPF C# code that I’d like to write in a “fluent” manner.

For example, I might want to setup a Window containing a ScrollViewer:

new Window()
    .SetContent(
        new ScrollViewer()
            .SetContent(
                ...))

To achieve this sort of API, I’ve been using extension methods such as these:

static class FluentScrollViewer
{
    public static ScrollViewer SetContent(this ScrollViewer obj, object val)
    { obj.Content = val; return obj; }
}

static class FluentWindow
{
    public static Window SetContent(this Window obj, object val)
    { obj.Content = val; return obj; }
}

Now, Window and ScrollViewer both inherit the Content property from ContentControl. Yet, I’ve had to define the SetContent extension methods for each class separately.

If I try to do something like this instead:

static class FluentContentControl
{
    public static ContentControl SetContent(this ContentControl obj, object val)
    { obj.Content = val; return obj; }
}

and then use it like so:

new Window().SetContent(...)

the SetContent method doesn’t return a Window of course.

Is there way to define SetContent over ContentControl and have it do the “right thing” so as to avoid defining lots of individually speciallized methods which are similar except for the type?

  • 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-28T08:30:26+00:00Added an answer on May 28, 2026 at 8:30 am

    You can use generics:

    public static TControl SetContent<TControl>(this TControl obj, object val)
    where TControl : ContentControl
    { 
        obj.Content = val; 
        return obj; 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Occasionally, I will write some code with way more parentheses than I like. if(!(new
Occasionally I like to spend some time looking at the .NET code just to
Occasionally I have to do some profiling work on Java code, and I would
Occasionally it would be preferable to have some initialization code for palettes (of buttons).
Occasionally I will have some badly formed PHP code and I will get a
Occasionally I'll have a situation where I've written some code and, based on its
Occasionally I come accross a unit test that doesn't Assert anything. The particular example
Occasionally I find code which tests if two NSString s are the same like
Occasionally I am looking at some code, I search for usages of a method
We occasionally have bugs that appear once in every X runs. Before people check

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.