0

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

Clemens
  • 123,504
  • 12
  • 155
  • 268
  • Be aware that it is still wrong to set the UserControl's DataContext explicitly. Change your question so that it asks for how to correctly set the design-time DataContext. Do not just ask the same closed question again. It will again be closed. – Clemens Aug 08 '22 at 13:29
  • @Clemens Thank you very much. But there is no answer for my question in associated question. Should I change Header or all my question should be reorgonized? I didnt removed explicit data context setting because may be it is another solution. – Bukeyev Eldar Aug 08 '22 at 13:52
  • The answer is not to set the DataContext. A UserControl must never do that. You need to ask how to set `d:DataContext`. – Clemens Aug 08 '22 at 13:53
  • @Clemens associated question has no one possible solution for my question. How I should to ask it. – Bukeyev Eldar Aug 08 '22 at 13:56
  • You could make your code work with `DataContext="{Binding DataContext.ViewContol1ViewModel, RelativeSource={RelativeSource AncestorType=Window}}"`, but that is very bad practice. And I did not tell you about it. Seriously, get the design-time DataContext up and running. – Clemens Aug 08 '22 at 13:56
  • Yeah, you helped me a lot. I think I should change topic of my question. And write about VS errors. – Bukeyev Eldar Aug 08 '22 at 13:58
  • @Clemens, Hello, your solution works, Thank you again:) I found solution for fixing xaml editor namespace issue on StackOverflow as you said and decided not to reopen my question. – Bukeyev Eldar Aug 09 '22 at 07:03

0 Answers0