troubleshooting Question

Data bind to a Shared Property WPF

Avatar of p-plater
p-platerFlag for Australia asked on
.NET ProgrammingVisual Basic.NET
6 Comments1 Solution260 ViewsLast Modified:
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

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

<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}"/>

What am I doing Wrong?

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros