We help IT Professionals succeed at work.
Get Started

Data bind to a Shared Property WPF

Last Modified: 2016-02-20
I have a Class with Shared Properties
This Class holds the Name and ID of the Current User.
It all works fine until I try to databind to the Shared Values.

When I update the Shared Values in Code the Displayed Values stay the same.
But if I update the Displayed Values (In the Text box) the Bound Shared Properties DO update.

Public Class CurrentUser
    Inherits CommonViewModelBase

    Private Shared _name As String = "No Name"
    Private Shared _ID As Integer
    Private Shared _thisInstance As CurrentUser

    Public Shared Property name As String
            Return _name
        End Get
        Set(value As String)
            _name = value
        End Set
    End Property

    Public Shared Property ID As Integer
            Return _ID
        End Get
        Set(value As Integer)
            _ID = value
        End Set
    End Property

    Protected Sub New()
        'initialization code goes here

    End Sub

    Public Shared Function GetCurrentUser() As CurrentUser
        ' initialize object if it hasn't already been done
        If _thisInstance Is Nothing Then
            _thisInstance = New CurrentUser
        End If
        ' return the initialized instance
        Return _thisInstance
    End Function

End Class

Open in new window

This Property is in the View Model of the Main Window
Public Property localCurrentUser As CurrentUser = CurrentUser.GetCurrentUser

Open in new window

<StackPanel Orientation="Horizontal" Grid.Row="4" HorizontalAlignment="Right" DataContext="{Binding localCurrentUser}">
            <TextBlock Name="StaffNameTextBlock1" Text="Staff Name (Click to Change) "/>
            <TextBlock x:Name="StaffNameTextBlock" Text="{Binding name, Mode=TwoWay, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"/>

Open in new window

What am I doing Wrong?
Watch Question
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE