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)
{
this.DataContext = new WidgetViewModel();
}
}
}
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"
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 . , ? , ...
? , :)).