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 6229805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:38:12+00:00 2026-05-24T09:38:12+00:00

In a code behind XAML file, if my DataContext will always be a well-known

  • 0

In a code behind XAML file, if my DataContext will always be a well-known class, i usually redeclare DataContext like this:

public new Cow DataContext
{
    get { return base.DataContext as Cow; }
    set { base.DataContext = value; }
}

This way i have Intellisense treating DataContext as a Cow in the scope of my file and I am safely (at least i think so) using the base.DataContext to handle all its logic.

Some people, with lots of experience, have already told me that this is not a good pattern.

I’d like to know what problems could arise and why not use it. I’ve already given it a lot of thought and couldn’t think of one

Thank you

  • 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-24T09:38:13+00:00Added an answer on May 24, 2026 at 9:38 am

    After pondering for a while about that code I cannot find any serious issue, except perhaps for this corner case that I will explain.

    Let’s say your Xaml belongs to a class called MyUserControl, and that it inherits from UserControl.

    Let’s assume you have:

    UserControl ctrl = new MyUserControl();
    ctrl.DataContext = new Dog();
    

    This is obviously legal and you are not protected by MyUserControl’s type-safety, because the type of the reference variable is of UserControl, whose DataContext property is still of object type. However, let’s immagine down the line, you try the following, in MyUserControl’s context:

    Cow c = this.DataContext;
    

    c will be null. This is what you intended. However, it might make your life harder under a debugging scenario because you are being lulled into thinking your DataContext is null, while it simply isn’t what you expected it to be. It might be harder to understand why your data bindings are all working funny while your datacontext seems to be null.

    My approach to this is not to hide the DataContext property, but to re-expose it via a new property:

    public Cow Model
    {
        get
        {
            return this.DataContext as Cow;
        }
        set
        {
            this.DataContext = value;
        }
    }
    

    In the scenario I set up above, while the Model property would return null, the DataContext would return a reference to the Dog instance, more appropriately reflecting what is really going on. Alternatively, you might implement your DataContext property’s getter by returning the following:

    return (Cow)this.DataContext;
    

    At least you’d receive an exception telling you DataContext does have something, but not what you want. It does seem cheesy, however.

    Anyhow, this is really no serious issue, only a possible inconvenience under the scenario I laid out.

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

Sidebar

Related Questions

In code-behind of an ASP.NET page I have this method: public string TestFunc() {
Does the XAML file and its .cs code behind run on server-side or client-side?
I have this code in my code-behind file of my View: private string GetSelectedSchoolclassCode()
I moved a silverlight file (xaml) to a different project, and now code behind
How can I get a named resource from ControlTemplate in a code-behind file (*.xaml.cs)?
Bellow is the code behind and the Xaml for a demo app to review
Is it possible to create a Line in XAML (without any C# code behind)
I have some object that is instantiated in code behind, for instance, the XAML
Code Behind: [WebMethod] public static string emp() { return BlaBla; } Aspx Page: $(document).ready(function()
The code behind file creates a list of employees and the asp.net page loops

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.