vb.net programmatically added binding not working - wpf

I am trying to bind a variable value to a button's content property. i created a button named "button" inside a dockpanel of my main window in XAML.
<Button x:Name="button" Content="Button" Height="100"
VerticalAlignment="Top" Width="75"/>
Then I want to add a binding to a public variable test programmatically.
The initial value (400) is displayed correctly at runtime, but when I hit the "NextTurn" button to raise the Click event, the bound value isn't updated.
Imports System.Windows.Data
Class MainWindow
Public test As Integer
Public Sub New()
InitializeComponent()
Dim usr As New UserNS.User
mainUser = usr
test = 400
Dim btest As New Binding()
btest.Source = test
button.SetBinding(Button.ContentProperty, btest)
End Sub
Private Sub NextTurn_Click(sender As Object, e As RoutedEventArgs) Handles NextTurn.Click
test = test - 10
End Sub
End Class
Could you please help me?
Thank you very much!

First of all, fields cannot be bound, only properties.
The binding source should be an object which has the property you would like to bind.
Ideally it is not the form class itself but a separate class (aka. view model).
E.g. the main window (named MainWindow) can have a view model named MainViewModel.
This object must implement the INotifyPropertyChanged interface.
In the property setter you have to call a method which raises the PropertyChanged event that comes with INotifyPropertyChanged interface.
In my example it is:
Private Sub NotifyPropertyChanged(...)
IMPORTANT: VB.NET works in case-insensitive mode so avoid naming a Button control as button. Also if you implement a full property the backing field should have a different name. You cannot have a test field and a Test property at the same time. That's why I chose the _Test name for the field.
Here is a working example:
Imports System.ComponentModel
Imports System.Runtime.CompilerServices
Class MainWindow
Implements INotifyPropertyChanged
Public Sub New()
' Actually we can initialize the Test property here as well.
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Test = 400
Dim bindingTest As New Binding() With {
.Source = Me, ' The object which has the property we want to bind.
.Mode = BindingMode.OneWay, ' We declare that the UI will accept changes from the object's property but not vica-versa.
.Path = New PropertyPath("Test") 'We have to pass the name of the property as a String value.
}
TestButton.SetBinding(Button.ContentProperty, bindingTest)
' We could also initialize the Test property here.
End Sub
' We can also initialize only the field instead of the property
' But new values must be set through the property setter.
Private _Test As Integer
Public Property Test() As Integer
Get
Return _Test
End Get
Set(ByVal value As Integer)
_Test = value
NotifyPropertyChanged()
End Set
End Property
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
' We use CallerMemberName attribute so that we do not need to pass the name of the property.
' The compiler will automatically pass the name of the caller property. In our case: "Test"
' To get it work we declare the parameter as Optional so that we really do not have to pass a parameter value.
Private Sub NotifyPropertyChanged(<CallerMemberName()> Optional ByVal propertyName As String = "")
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
Private Sub NextTurnButton_Click(sender As Object, e As RoutedEventArgs)
' You must set the property's value instead of the backing field!
Test = Test - 10
End Sub
End Class

Related

WPF Dependency Property - set from XAML

I am trying to achieve the following in a WPF personal finance app:
In various places I want to display a user control giving details of a asset holding (usually a share, bond etc), the target asset may be changed dynamically by the user in which case the control must be refreshed. Each Asset has a unique identifier, AssetId.
I am using MVVM and I've developed a single window with a View Model that takes AssetID as a parameter (property) and retrieves the relevant details for binding to the View. This work fine. What I'd like to do is make a generic user control with the same functionality so I can basically drop that 'window' inside other windows.
So I pretty much copy-pasted the XAML from that form into a User Control, where I'm struggling is passing in the AssetId from the parent window to the child control.
Google tells me I need a dependency property and here's where I am
Public Class HoldingView
Private _AssetId As Integer
Public AssetIdProperty As DependencyProperty = DependencyProperty.Register("AssetId",
GetType(Integer),
GetType(HoldingView),
New FrameworkPropertyMetadata(New PropertyChangedCallback(AddressOf AssetIDChanged)))
Public Property AssetId As Integer
Get
Return GetValue(AssetIdProperty)
End Get
Set(value As Integer)
SetValue(AssetIdProperty, value)
End Set
End Property
Private Sub AssetIDChanged(d As DependencyObject, e As DependencyPropertyChangedEventArgs)
Dim NewAssetId As Integer
NewAssetId = e.NewValue
Me.DataContext.AssetId = NewAssetId
End Sub
Public Sub New()
' This call is required by the designer.
InitializeComponent()
Me.DataContext = New HoldingViewmodel
End Sub
End Class
Called like this:
<Grid>
<local:HoldingView AssetId="{Binding AssetId}"/>
</Grid>
The code compiles and runs but when I try and load the window that has the user control, the app crashes with this message:
A 'Binding' cannot be set on the 'AssetId' property of type 'HoldingView'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
Which is not that helpful. From my Googling, you can also get this message if the syntax of the DP registration is not spot on, but it looks Ok to my inexperienced eye...
Anybody else had this?
Public AssetIdProperty As DependencyProperty
should be
Public Shared ReadOnly AssetIdProperty As DependencyProperty
Please take a look at Custom Dependency Properties.
Also remove
Me.DataContext = New HoldingViewmodel
because that will effectively break any DataContext-based Bindings like
AssetId="{Binding AssetId}"
where the source property is supposed to be owned by the object in the inherited DataContext, which usually is an object in the application's view model.
Controls should never have their own, "private" view model, but instead handle property changes in code behind. In case of UserControls, there could simply be UI elements in their XAML that would be bound to the UserConrol's own properties.
Hence
Me.DataContext.AssetId = NewAssetId
in the PropertyChangedCallback is pointless and should be removed, as well as
Private _AssetId As Integer
To summarize, it should look like this:
Public Class HoldingView
Public Shared ReadOnly AssetIdProperty As DependencyProperty =
DependencyProperty.Register(
"AssetId",
GetType(Integer),
GetType(HoldingView),
New FrameworkPropertyMetadata(
New PropertyChangedCallback(AddressOf AssetIdPropertyChanged)))
Public Property AssetId As Integer
Get
Return GetValue(AssetIdProperty)
End Get
Set(value As Integer)
SetValue(AssetIdProperty, value)
End Set
End Property
Private Shared Sub AssetIdPropertyChanged(
d As DependencyObject, e As DependencyPropertyChangedEventArgs)
CType(d, HoldingView).AssetIdChanged(e.NewValue)
End Sub
Private Sub AssetIdChanged(id As Integer)
...
End Sub
Public Sub New()
InitializeComponent()
End Sub
End Class

ListView with grouping and sorting not refresh while INotifyPropertyChanged used

I have my own class which uses INotifyPropertyChanged correctly (Raising updates events), but when a property of type DateTime updated and called (View.Run) the listView not updating untill another property changing (not this property)
Now with the code:
Public Class EntryInfo
Implements INotifyPropertyChanged
ReadOnly Property DateAccessed As Date
Get
.......
Return _Access
End Get
End Property
Readonly Property Property1 as object
Get
.......
Return _Property1
End Get
End Property
Friend Sub NotifyPropertyChanged(ByVal info As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
RaiseEvent ApropertyHasChanged()
End Sub
Then when I need to Change the "DateAccessProperty" I use this code:
Friend Sub SetAccessTime(Dt As Date)
_Access = Dt
NotifyPropertyChanged("DateAccessed")
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''
After this I have a ListView named "LV1"
Dim Coll as new observableCollection(Of EntryInfo)
....... filing "Coll" with items (EntryInfo)
Lv1.ItemsSource =Coll
Then I do the following:
Do some sort and group operations.
Changing "DateAccessed" value. so that the "ApropertyHasChanged" event fired and at this point I used the following code
Private Sub RefreshViewNow()
Dim _view As ListCollectionView = TryCast(CollectionViewSource.GetDefaultView(LV1.ItemsSource), ListCollectionView)
If _view IsNot Nothing Then _view.Refresh()
'\\\ Items.Refresh()
End Sub
But _view not refreshed.
But if the property "Property1" changed the _View refreshed.
Any help?
The solution is by set the following "_view" properties:
_view.IsLiveFiltering = True
_view.IsLiveGrouping = True
_view.IsLiveSorting = True
or at least one of them if you want one of them only to be activated.

WPF Textbox Binding. VB.Net 2017

Scenario:
A structure is associated with an indexed random access file.
Data is loaded from the file in the constructor at boot time , and the structure is automatically assigned the data values from the file, and the textboxes in the WPF window is automatically populated.
The structure is also updated if the user enter new values in a textbox. So far so good.
The problem arise when the user loads a different dataset from the file when the program is running. This happen in a Button Click event. The structure is updated, but this change is not reflected in the textboxes.
Can someone hopefully shed some light on this.
Pretty new to both WPF and VB.Net
Public Class MainWindow
Shared myStruct As aStruct
Public Structure aStruct
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Public year As Integer
Public Property YearP As Integer
Get
Return myStruct.year
End Get
Set(value As Integer)
myStruct.year = value
End Set
End Property
Public salary as Integer
Public Property SalaryP As Integer
Get
Return myStruct.salary
End Get
Set(value As Integer)
myStruct.salary = value
End Set
End Property
.......
.......
End Structure
Public Sub New()
Me.New(".\indxfile.ptx", ".\datafile.dat")
' This call is required by the designer.
'InitializeComponent() ' Moved
' Add any initialization after the InitializeComponent() call.
End Sub
Public Sub New(Optional _IndexFileName As String = "", Optional _DataFileName As String = "")
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.DataContext = myStruct
Dim Retval As Integer
Retval = GetRecord(2018, DataFileName, Len(myStruct), myStruct, IndexFilename)
If Retval <> 0 Then
MsgBox("Error")
End If
End Class
<TextBox x:Name="Year" Text = "{Binding YearP, Mode = TwoWay}"
Your 'Set' properties need to invoke the PropertyChanged event so that the data binding updates.

Correctly bind object instance property to a Label in code

This is supposed to be easy, yet I can't get it to work.
This is a simplified example so I can illustrate my problem. I have a List(Of Object) and I want to bind the Content of a Label to some property of an object in the list. I want to do this in code (Because those labels will be generated in runtime).
I create my object that will hold the value for the label and the list, that will hold those objects:
' The List to hold objects
Public Class BList
Public Shared listy As New List(Of BindTest)
End Class
' Object to hold label text
Public Class BindTest
Public Property Namy As String
End Class
Then I try to create the object and add it to the list. And try to set the binding for the label (for sake of simplicity, lets say I want bind to the first list item).
Dim bb As New BindTest
bb.Namy = "FirstName"
BList.listy.Add(bb)
B_label.SetBinding(Label.ContentProperty, "Namy")
B_label.DataContext = BList.listy.Item(0)
So far it works fine and label shows "FirstName" as expected. But then if I try to change the value of that first item like this:
BList.listy.Item(0).Namy = "Something else"
nothing happens and the label is not updated.
Thanks to Mike Eason.
I needed to implement INotifyPropertyChanged. I somehow thought, that it would be implemented automatically with all that WPF default-way of databinding everything. Oh well, one needs implement this manually.
So for the code to work, this part:
' Object to hold label text
Public Class BindTest
Public Property Namy As String
End Class
..must be changed to this:
' Object to hold label text
Public Class BindTest
Implements INotifyPropertyChanged
Private _Namy As String
Public Property Namy
Set(value)
_Namy = value
_PropertyChanged("Namy")
End Set
Get
Return _Namy
End Get
End Property
Private Sub _PropertyChanged(Optional ByVal PropertyName As String = Nothing)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(PropertyName))
End Sub
Private Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
End Class

VB.net WPF DataGrid ObservableCollection Binding property update

I am using VB.NET and WPF within Visual Studio 2010 Express.
Currently, I have:
A DataGrid by the name of downloadListDG. This has a column which is a template containing an image.
An ObservableCollection of a custom DownloadListItem class.
This DownloadListItem has a public property which is another custom class.
This class has a private dim which is a StateType (a custom enum), and a public readonly property which returns a string depending on what the StateType is (actually an image URI if you're curious).
The DownloadListItem also has a public property which just returns the StateType (this is just for binding purposes)
My problem is that whenever the StateType changes, the image column in the DataGrid does not change. I have been trying to use the IPropertyChangedNofity, but nothing changes, so either I'm using it incorrectly or I need to use another method.
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
AddHandler ControllerRef.StateChanged, AddressOf StateChangeHandler
Private Sub StateChangeHandler(NewState As State)
MsgBox(NewState)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("CurrentState"))
End Sub
Thanks in advance
Make sure the PropertyChanged event is notifying the UI of the property name you are bound to, not the property that triggers the change. Example:
Imports System.ComponentModel
Public Class DownloadListItem : Implements INotifyPropertyChanged
Friend Enum StateEnum
State1 = 0
State2 = 1
End Enum
Private _CurrentState As StateEnum
Private Sub ChangeEnumValue(NewValue As StateEnum)
_CurrentState = NewValue
OnPropertyChanged("ImageURI")
End Sub
Public ReadOnly Property ImageURI As String
Get
' TODO: Implement conditional logic to return proper value based on CurrentState Enum
End Get
End Property
Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
Protected Sub OnPropertyChanged(PropertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(PropertyName))
End Sub
End Class

Resources