[wpf][xaml][vb]Data Binding
[wpf][xaml][vb]Data Binding
[wpf][xaml][vb]Data Binding
[wpf][xaml][vb]Data Binding
[wpf][xaml][vb]Data Binding
[wpf][xaml][vb]Data Binding [wpf][xaml][vb]Data Binding [wpf][xaml][vb]Data Binding [wpf][xaml][vb]Data Binding [wpf][xaml][vb]Data Binding [wpf][xaml][vb]Data Binding [wpf][xaml][vb]Data Binding [wpf][xaml][vb]Data Binding
[wpf][xaml][vb]Data Binding [wpf][xaml][vb]Data Binding
[wpf][xaml][vb]Data Binding
Go Back  Xtreme Visual Basic Talk > > > [wpf][xaml][vb]Data Binding


Reply
 
Thread Tools Display Modes
  #1  
Old 12-25-2013, 12:27 AM
JeremyBenson11 JeremyBenson11 is offline
Freshman
 
Join Date: Dec 2013
Posts: 48
Default [wpf][xaml][vb]Data Binding


I'm trying to figure out data binding but I'm having a bit of trouble. I have an xaml file with a textblock attached to a statusbar. A class called StatusBarClass in a folder called Classes and then my main window code. I'm trying to bind the property statusText of my status bar class to my textblock..

main window.xaml
Code:
<StatusBar DockPanel.Dock="Bottom">
                <TextBlock  x:Name="StatusBar" Text="{Binding }"></TextBlock>
            </StatusBar>
StatusBarClass.vb
Code:
Private statusText As String

    Public Sub setStatus(ByVal value As String)
        statusText = value
    End Sub

    Public Function returnStatus() As String
        Return statusText
    End Function

End Class
mainwindow.xaml.vb
This is the portion of the code where the object is called and used.
Code:
 Case 1
                Dim spellName As String
                spellName = "fireball"
                WordProcessor.AppendText(spellName)
                Dim StatBar = New StatusBarClass
                StatBar.setStatus("Status: Generating Spell")
Reply With Quote
  #2  
Old 12-25-2013, 02:53 AM
JeremyBenson11 JeremyBenson11 is offline
Freshman
 
Join Date: Dec 2013
Posts: 48
Default

I learned a bit about dependency properties and property wrappers. I had to change my class around quite a bit, but still really foggy on the subject though. I can't seem to figure out how to get the value in my textblock. I'll paste the new code...

StatusBarClass

Code:
Public Class StatusBarClass
    Inherits TextBlock
    Public Property statusText As String
        Get
            Return GetValue(statusTextProperty)
        End Get
        Set(ByVal value As String)
            SetValue(statusTextProperty, value)
        End Set
    End Property

    Public Shared statusTextProperty As DependencyProperty = DependencyProperty.Register("statusTextProperty", _
                                                                          GetType(String), _
                                                                          GetType(TextBlock), _
                                                                          New FrameworkPropertyMetadata(""))
   

End Class
Xaml

Code:
<StatusBar DockPanel.Dock="Bottom">
                <TextBlock  x:Name="StatusBar" Text="{Binding Path=StatusBarClass.statusText}"></TextBlock>
            </StatusBar>
Main Window

Code:
Case 8
                Dim spellName As String
                spellName = "flame strike"
                WordProcessor.AppendText(spellName)
                Dim StatBar = New StatusBarClass
                StatBar.statusText = New String("Status: Generating Fire Elemental Spell")
Reply With Quote
  #3  
Old 12-25-2013, 12:19 PM
JeremyBenson11 JeremyBenson11 is offline
Freshman
 
Join Date: Dec 2013
Posts: 48
Default

I've updated the code a little based on things I'm reading across Microsoft docs and code projects, but still no good. Learned about inotifychange and what not...

I feel like I'm getting closer, but there's a lot in the code I don't understand. Kill me, lol.

Status Bar Class
Code:
Imports System.ComponentModel

Public Class StatusBarClass
    Inherits TextBlock
    Implements INotifyPropertyChanged

    Sub New(ByVal status As String)
        Me.statusText = status
    End Sub

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Public Property statusText As String
        Get
            Return GetValue(statusTextProperty)
        End Get
        Set(ByVal value As String)
            SetValue(statusTextProperty, value)
            OnPropertyChanged()
        End Set



    End Property

    Public Shared statusTextProperty As DependencyProperty = DependencyProperty.Register("statusTextProperty", _
                                                                          GetType(String), _
                                                                          GetType(TextBlock), _
                                                                          New FrameworkPropertyMetadata(""))


    Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
End Class
I also updated a bit of my mainwindow xaml

Code:
<StatusBar DockPanel.Dock="Bottom">
                <TextBlock  x:Name="StatusBar" Text="{Binding Path=StatusBarClass.statusText, Mode=OneWay}"></TextBlock>
            </StatusBar>
Main window vb is the same

Code:
Case 8
                Dim spellName As String
                spellName = "flame strike"
                WordProcessor.AppendText(spellName)
                Dim StatBar = New StatusBarClass
                StatBar.statusText = New String("Status: Generating Fire Elemental Spell")
Reply With Quote
  #4  
Old 12-25-2013, 12:50 PM
JeremyBenson11 JeremyBenson11 is offline
Freshman
 
Join Date: Dec 2013
Posts: 48
Default

I've changed my code yet again. Maybe someone could tell me which way I'm closer.

class

Code:
Imports System.ComponentModel

Public Class StatusBarClass
    Implements INotifyPropertyChanged

    Public statusText As String


    Sub setStatus()
    End Sub

    Sub setStatus(ByVal status As String)
        Me.statusText = status
    End Sub

    ' Declare the event
    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Public Property status() As String
        Get
            Return statusText
        End Get
        Set(value As String)
            statusText = value
            ' Call OnPropertyChanged whenever the property is updated
            onPropertyChanged("status")
        End Set
    End Property

    Public Sub onPropertyChanged(ByVal status As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(statusText))
    End Sub

End Class
main window
Code:
 Case 1
                Dim spellName As String
                spellName = "fireball"
                WordProcessor.AppendText(spellName)
                Dim StatBar = New StatusBarClass
                StatBar.setStatus("Status: Generating Fire Elemental Spell")
main window xmal

Code:
Code:
<StatusBar DockPanel.Dock="Bottom">
                <TextBlock  x:Name="StatusBar" Text="{Binding Path=StatusBarClass.statusText, Mode=OneWay}"></TextBlock>
            </StatusBar>

Last edited by JeremyBenson11; 12-25-2013 at 01:25 PM.
Reply With Quote
  #5  
Old 12-28-2013, 07:31 PM
PlausiblyDamp's Avatar
PlausiblyDamp[wpf][xaml][vb]Data Binding PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,056
Default

The bit of code
Code:
Text="{Binding Path=StatusBarClass.statusText, Mode=OneWay}"
is trying to bind to a property of the StatusBarClass class rather than an instance of the class, you would either need to create a resource whose type is StatusBarClass or assign an instance of the class to the control's (or to something that contains the control) DataContext.

e.g. in the form load for your main window do something like
Code:
Me.DataContext = new StatusBarClass ' sets the datacontext for window and all controls on the window
'or
<name of status bar control>.DataContext = new StatusBarClass
If you are seriously using WPF then I strongly suggest you read up on MVVM as a design pattern, although it can initially seem complex it can make this kind of thing far easier and lead to far more testable code as well.

http://msdn.microsoft.com/en-us/magazine/dd419663.aspx is a good starting point.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
[wpf][xaml][vb]Data Binding
[wpf][xaml][vb]Data Binding
[wpf][xaml][vb]Data Binding [wpf][xaml][vb]Data Binding
[wpf][xaml][vb]Data Binding
[wpf][xaml][vb]Data Binding
[wpf][xaml][vb]Data Binding [wpf][xaml][vb]Data Binding [wpf][xaml][vb]Data Binding [wpf][xaml][vb]Data Binding [wpf][xaml][vb]Data Binding [wpf][xaml][vb]Data Binding [wpf][xaml][vb]Data Binding
[wpf][xaml][vb]Data Binding
[wpf][xaml][vb]Data Binding
 
[wpf][xaml][vb]Data Binding
[wpf][xaml][vb]Data Binding
 
-->