We help IT Professionals succeed at work.
Get Started

Data bind to a Shared Property WPF

251 Views
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.

Class
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
        Get
            Return _name
        End Get
        Set(value As String)
            _name = value
            _thisInstance.OnPropertyChanged(NameOf(name))
        End Set
    End Property

    Public Shared Property ID As Integer
        Get
            Return _ID
        End Get
        Set(value As Integer)
            _ID = value
            _thisInstance.OnPropertyChanged(NameOf(ID))
        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


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

Open in new window



What am I doing Wrong?
Comment
Watch Question
Commented:
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