Please tell me how to set something like "DataContextType" in UserControl.
In my case I have MainWindow.xaml with TabControl, where I set two TabItems. They have UserControl type.
<Window x:Class="MyProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyProject"
xmlns:views="clr-namespace:MyProject.Views"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="1080">
<Grid>
<TabControl>
<TabItem Header="Item 1" Width="auto" >
<views:ViewContol1 DataContext="{Binding ViewContol1ViewModel}"></views:ViewContol1>
</TabItem>
<TabItem Header="Item 2" Width="auto" >
<views:ViewContol2 DataContext="{Binding ViewContol2ViewModel}"></views:ViewContol2>
</TabItem>
</TabControl>
</Grid>
</Window>
DataContext for my MainWindow.xaml is MainViewModel.cs class instance. I setting it in MainWindow.xaml.cs. It has two "View Model" properties for each TabItem.
using Prism.Mvvm;
using System.Text;
namespace MyProject.ViewModels
{
public class MainViewModel : BindableBase
{
public ViewContol1ViewModel ViewContol1ViewModel { get; set; }
public ViewContol2ViewModel ViewContol2ViewModel { get; set; }
public MainViewModel()
{
ViewContol1ViewModel = new ViewContol1ViewModel();
ViewContol2ViewModel = new ViewContol2ViewModel();
}
}
}
And it is UserControl1.xaml, where I setting DataContext second time to be able to see it's ViewModel's (ViewContol1ViewModel type instance) variables in XAML editor.
<UserControl x:Class="MyProject.Views.ViewContol1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MyProject.Views"
xmlns:viewmodels="clr-namespace:MyProject.ViewModels"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.DataContext>
<viewmodels:ViewContol1ViewModel></viewmodels:ViewContol1ViewModel>
</UserControl.DataContext>
<Grid>
</Grid>
</UserControl>
My solution makes two instance of ViewContol1ViewModel class. First when initializes UserControl and second when I set it's DataContext in my MainWindow.
So I want to know if it's possible to set type of User Control's DataContext type to certain class type, to see properties of DataContext to which I want to bind my View elements in UserControl.
I used DesignInstance, but VS doesn't want to compile my project.
Screen1
It said like there is no this class in namespace but it actually is, an VS it shows:
Screen2