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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:20:47+00:00 2026-05-11T21:20:47+00:00

I am designing a WPF user control which contains other user controls (imagine a

  • 0

I am designing a WPF user control which contains other user controls (imagine a WidgetContainer, containing different Widgets) – using M-V-VM architecture.
During development, I have WidgetContainerView in a window, window (View) spawns a WidgetContainerViewModel as its resource, and in a parameterless constructor of WidgetContainerViewModel, I fill its exposed collection with some sample widgets (WidgetViewModels).

WidgetContainer control inherits the DataContext from window, and inside, there is a ListView, that binds Widgets to WidgetView control (which is inside ListView.ItemTemplate).

Now this works OK in my WindowView, as I see my sample widgets, but once I edit the WidgetContainerView or WidgetView, there is no content – at design time, controls are standalone, and they don’t inherit any DataContext, so I don’t see a content, and have troubles designing them (a ListView is empty, Widget’s fields as well…).

I tried adding a sample widget to the WidgetView:

public partial class WidgetView : UserControl
{
    public WidgetView()
    {
        InitializeComponent();
        if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
        {
            //btw, MessageBox.Show(...) here sometimes crashes my Visual Studio (2008), but I have seen the message - this code gets executed at design time, but with some lag - I saw the message on reload of designer, but at that time, I have already commented it - wtf?
            this.DataContext = new WidgetViewModel(); //creates sample widget
        }
    }
}

but that didn’t work – I still don’t see anything in designer.

I also wanted to create a WidgetViewModel as a resource in WidgetView, like this:

<UserControl x:Class="MVVMTestWidgetsControl.View.WidgetView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    DataContext="WidgetViewModel" //this doesn't work!
    Height="Auto" Width="Auto">
    <UserControl.Resources>
        <ResourceDictionary>
            <ViewModel:WidgetViewModel x:Key="WidgetViewModel" />
        </ResourceDictionary>
    </UserControl.Resources>

    <TextBlock Text="{Binding Path=Title}"></TextBlock>

</UserControl>

but I don’t know how to assign a WidgetViewModel as a DataContext of a whole widget – I can’t add DataContext attribute to UserControl, because WidgetViewModel is defined later in the code. Any ideas how to do this? I could use a sample data this way, and just override it in code so that it has the right content at runtime…

What are your best practices when developing user controls? Thank you, designing empty control is no fun :)).

  • 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-11T21:20:47+00:00Added an answer on May 11, 2026 at 9:20 pm

    In your second snippet, you should be able to refer to your DataContext as a DynamicResource:

    DataContext="{DynamicResource WidgetViewModel}"
    

    But most custom user controls have some sort of top level layout container, and you can set the DataContext on that container as a StaticResource.

    In your case, however, you may want to consider dropping the VM portion of your code altogether since you’re writing a custom UserControl. You should ask yourself what benefits are you gaining from a completely self-contained ViewModel with no real backing Model designed for just one View (i.e. the custom UserControl). Perhaps you could just define some DependencyProperties and use those?

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

Sidebar

Related Questions

We are currently designing a system using WPF which will communicate with other systems
I'm currently designing a smart client app (WPF) which needs to operate in an
I'm trying to learn how to design an application which has several different user
I am using a WPF tab control to present separate repeated instances of a
I'm designing a WPF program that will use a DLL-plugin architecture. The host application
I'm slowly learning WPF using this article and other resources. I am focusing on
Well I'm designing a Custom WPF control - fore the sake of learning -
We're designing a WPF / MVVM application that allows the user to search and
When designing WPF application UI's in XAML, should every container-type control contain a layout
I'm designing a game and thinking about using WPF for making a simple prototype

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.