Designing a WPF UserControl that gets its DataContext from external controls: how to have some sample data in the designer, but use the inherited DC at run time?

I am creating a WPF user control that contains other user controls (imagine a WidgetContainer containing various widgets) - using the MV-VM architecture. During development, I have a WidgetContainerView in the window, the window (View) spawns the WidgetContainerViewModel as my resource, and in the constructor without parameters WidgetContainerViewModel I populate my open collection with some sample widgets (WidgetViewModels).

The WidgetContainer control inherits the DataContext from the window, and inside there is a ListView that associates the Widgets controls with the WidgetView (which is inside the ListView.ItemTemplate).

Now it works fine in my WindowView, since I see my sample widgets, but as soon as I edit the WidgetContainerView or WidgetView, there is no content during development - the controls are autonomous and they don’t inherit any DataContext, so I don’t see the content and do not create problems (ListView is empty, widget fields ...).

I tried adding a widget sample to a 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 it didn’t work - I still don’t see anything in the designer.

I also wanted to create a WidgetViewModel as a resource in a WidgetView, for example:

<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>

, WidgetViewModel DataContext - DataContext UserControl, WidgetViewModel . , ? , ...

? , :)).

+3
2

DataContext DynamicResource:

DataContext="{DynamicResource WidgetViewModel}"

, DataContext StaticResource.

, , VM , UserControl. , ViewModel . ( UserControl). , DependencyProperties ?

+3

: DC ( ) View codebehind:

    public PanelView()
    {
        InitializeComponent();

        if (!DesignerProperties.GetIsInDesignMode(new DependencyObject())) //DeleteAtRelease:
        {
            //we are in runtime, reset DC to have it inherited
            this.DataContextHolder.DataContext = DependencyProperty.UnsetValue;
        }

    }

DC , VS - , , .

:

        if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
        {
            this.DataContext = new WidgetViewModel();
        }
+1

Source: https://habr.com/ru/post/1710514/


All Articles