How to Link MVVM Elements

RogerSchlueter-7899 1,346 Reputation points
2022-12-19T05:55:06.707+00:00

I'm working on my first project using the MVVM pattern and can't get the various parts to work together nicely.

The project is named PIM. Under that I have directories named ViewModels and Views.

First problem: I can't get my Views to recognize the corresponding View Models. Here is the relevant code for the MainWindow:

<Window  
    x:Class="MainWindow"  
    xmlns:self="clr-namespace:PIM"  
	<...>  
    <Window.DataContext>  
        <self:MainWindowVM />  
    </Window.DataContext>  
	<...>  
</Window>  

and the view model:

Namespace PIM  
    Public Class MainWindowVM  
    <...>  
    End Class  
End Namespace  

But this yields the following error message:

The name "MainWindowVM" does not exist in the namespace "clr-namespace:PIM".

What am I missing?

Second problem: I can't get some resources to be recogniozed. In the Application.xaml file I have:

<Application  
    <...>  
    <Application.Resources>  
        <Style  
            x:Key="DateTimePickerBase"  
            TargetType="{x:Type xctk:DateTimePicker}">  
            <...>  
        </Style>  
        <Style  
            x:Key="LabelBase"  
            TargetType="Label">  
            <...>  
        </Style>  
        <...>  
    <Application.Resources>  
</Application>  

In one of my views I have:

<Window.Resources>  
    <...>  
    <Style  
        BasedOn="{StaticResource DateTimePickerBase}"  
        TargetType="{x:Type xctk:DateTimePicker}">  
        <Setters ....>  
    </Style>  
    <Style  
        BasedOn="{StaticResource LabelBase}"  
        TargetType="Label">  
        <Setters ....>  
    </Style>  
</Window.Resources>  

Here I get the error message that "DateTimePickerBase not found" but "LabelBase" does NOT yield that problem ... yet BOTH are in the same file. What's even worse, if I cut out that style completely and do a build, I still get the same error message even though that style is no longer included in the XAML.

I need some help.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,824 questions
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,331 Reputation points
    2022-12-20T07:23:39.773+00:00

    Hi,
    here my little demo. It works without problems.

    272320-x.jpg

    <Application x:Class="Application"  
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
                 xmlns:local="clr-namespace:PIM"  
                 xmlns:xctk="clr-namespace:WpfControlLibrary1;assembly=WpfControlLibrary1"  
                 StartupUri="MainWindow.xaml">  
        <Application.Resources>  
        <Style  
                 x:Key="DateTimePickerBase"  
                 TargetType="{x:Type xctk:DateTimePicker}">  
        </Style>  
        <Style  
                 x:Key="LabelBase"  
                 TargetType="Label">  
        </Style>  
      </Application.Resources>  
    </Application>  
      
    <Window x:Class="MainWindow"  
            xmlns:self="clr-namespace:PIM"  
            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:PIM"  
            xmlns:xctk="clr-namespace:WpfControlLibrary1;assembly=WpfControlLibrary1"  
            mc:Ignorable="d"  
            Title="MainWindow" Height="450" Width="800">  
      <Window.DataContext>  
        <self:MainWindowVM />  
      </Window.DataContext>  
      <Window.Resources>  
        <Style  
             BasedOn="{StaticResource DateTimePickerBase}"  
             TargetType="{x:Type xctk:DateTimePicker}">  
        </Style>  
        <Style  
             BasedOn="{StaticResource LabelBase}"  
             TargetType="Label">  
        </Style>  
      </Window.Resources>  
      <Grid>  
      
      </Grid>  
    </Window>  
      
      
    

2 additional answers

Sort by: Most helpful
  1. Hui Liu-MSFT 48,616 Reputation points Microsoft Vendor
    2022-12-19T09:20:28.267+00:00

    Hi,@ogerSchlueter-7899. Welcome Microsoft Q&A.
    As Peter said, there are default namespace (project name) in VB. You could try to refer to my sample code below. It works fine. The target platform of my project is .NET6.
    Project structure:
    272227-image.png
    App.xaml:

    <Application x:Class="Application"  
                ...  
                 xmlns:local="clr-namespace:PIM"  
                 xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"   
                 StartupUri="/Views/MainWindow.xaml">  
        <Application.Resources>  
            <Style   x:Key="DateTimePickerBase"  TargetType="{x:Type xctk:DateTimePicker}">  
                <Setter Property="Foreground"  
               Value="#FF333333" />  
            </Style>  
            <Style x:Key="LabelBase"    TargetType="Label">  
                <Setter Property="Foreground"  
               Value="#FF333333" />  
            </Style>  
      
        </Application.Resources>  
    </Application>  
    

    MainWindow.xaml:

    <Window x:Class="MainWindow"  
         ...  
            xmlns:self="clr-namespace:PIM"  
          xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"  >  
        <Window.DataContext>  
            <self:MainWindowVM/>  
        </Window.DataContext>  
        <Window.Resources>  
            <Style  BasedOn="{StaticResource DateTimePickerBase}"   TargetType="{x:Type xctk:DateTimePicker}">  
            </Style>  
            <Style  BasedOn="{StaticResource LabelBase}"   TargetType="Label">  
            </Style>  
        </Window.Resources>  
      
        <StackPanel >  
            <TextBox Text="{Binding MyTextProperty}"/>  
            <xctk:DateTimePicker Value="{Binding TodayDate}"></xctk:DateTimePicker>  
        </StackPanel>  
    </Window>  
    

    Complete code for MainWindow.xaml.vb:

    Class MainWindow  
      
    End Class  
    

    Complete code for MainWindowVM.vb:

    Imports System.ComponentModel  
      
    Public Class MainWindowVM  
            Implements INotifyPropertyChanged  
      
            Public Sub New()  
                Me.myTextValue = "default value..."  
            End Sub  
      
      
            Private myTextValue As String = String.Empty  
            Public Property MyTextProperty() As String  
                Get  
                    Return Me.myTextValue  
                End Get  
      
                Set(ByVal value As String)  
                    Me.myTextValue = value  
                    NotifyPropertyChanged("MyTextProperty")  
                End Set  
            End Property  
      
            Private _todayDate As DateTime = DateTime.Today  
            Public Property TodayDate() As DateTime  
                Get  
                    Return Me._todayDate  
                End Get  
      
                Set(ByVal value As DateTime)  
                    Me._todayDate = value  
                    NotifyPropertyChanged("TodayDate")  
                End Set  
            End Property  
      
            Public Event PropertyChanged As PropertyChangedEventHandler _  
                Implements INotifyPropertyChanged.PropertyChanged  
      
            Private Sub NotifyPropertyChanged(ByVal propertyName As String)  
                RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))  
            End Sub  
      
        End Class  
    

    The result:
    272228-image.png

    ----------------------------------------------------------------------------

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  2. Peter Fleischer (former MVP) 19,331 Reputation points
    2022-12-19T07:06:20.187+00:00

    Hi Roger,
    in VB.NET the default namespace is automatically inserted. Additional namespace in the code for MainWindowVM means that the MainWindowVM class is in the embedded namespace (BIM.BIM).

    Second problems resolves automatically after first rebuild (without errors).


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.