Let's admit a piece of code:
[...]
Private _filterContacts As FilterContacts
Public Property FilterContacts() As FilterContacts
Get
Return _filterContacts
End Get
Set(ByVal value As FilterContacts)
_filterContacts = value
OnPropertyChanged("FilterContacts")
End Set
End Property
Private _branchType As Nullable(Of Integer)
Public Property BranchType As Nullable(Of Integer)
Get
Return _branchType
End Get
Set(ByVal value As Nullable(Of Integer))
_branchType = value
OnPropertyChanged("BranchType")
End Set
End Property
[...]
Public Sub SomeSub()
FilterContacts.BranchType = BranchType
End Sub
I actually change the filter's "branchType", but I want to be notified that FilterContacts has changed, not just one of its fields. Is it possible? Thank you!
Set(ByVal value As Nullable(Of Integer))
_branchType = value
OnPropertyChanged("BranchType")
OnPropertyChanged("FilterContacts")
End Set
Or, if you want to invalidate all properties on the object, simply do this:
OnPropertyChanged("")
Related
I want to bind each property of my class "Artikelstammdaten" to a textbox, but my textbox stays empty. This is what the class looks like:
Public Class Artikelstammdaten
Private _Artikel As String
Private _BezeichnungDE As String
Private _BezeichnungEN As String
Private _Einheit As String
Private _MatGrp As String
Private _Kostenart As Integer
Private _Vertriebstext_DE As String
Private _Vertriebstext_EN As String
Private _Stuecklistennummer As String
Private _Status As String
Private _Klasse As String
Private _Mantelflaeche As Double
Private _Gewicht As Double
Private _KlasseID As String
Private _Stueckliste As IList(Of Stueckliste)
Private _Arbeitsgaenge As IList(Of Arbeitsgaenge)
Public Property Artikel As String
Get
Return _Artikel
End Get
Set
_Artikel = Value
End Set
End Property
Public Property BezeichnungDE As String
Get
Return _BezeichnungDE
End Get
Set
_BezeichnungDE = Value
End Set
End Property
Public Property BezeichnungEN As String
Get
Return _BezeichnungEN
End Get
Set
_BezeichnungEN = Value
End Set
End Property
Public Property Einheit As String
Get
Return _Einheit
End Get
Set
_Einheit = Value
End Set
End Property
Public Property MatGrp As String
Get
Return _MatGrp
End Get
Set
_MatGrp = Value
End Set
End Property
Public Property Kostenart As Integer
Get
Return _Kostenart
End Get
Set
_Kostenart = Value
End Set
End Property
Public Property Vertriebstext_DE As String
Get
Return _Vertriebstext_DE
End Get
Set
_Vertriebstext_DE = Value
End Set
End Property
Public Property Vertriebstext_EN As String
Get
Return _Vertriebstext_EN
End Get
Set
_Vertriebstext_EN = Value
End Set
End Property
Public Property Stuecklistennummer As String
Get
Return _Stuecklistennummer
End Get
Set
_Stuecklistennummer = Value
End Set
End Property
Public Property Status As String
Get
Return _Status
End Get
Set
_Status = Value
End Set
End Property
Public Property Klasse As String
Get
Return _Klasse
End Get
Set
_Klasse = Value
End Set
End Property
Public Property Mantelflaeche As Double
Get
Return _Mantelflaeche
End Get
Set
_Mantelflaeche = Value
End Set
End Property
Public Property Gewicht As Double
Get
Return _Gewicht
End Get
Set
_Gewicht = Value
End Set
End Property
Public Property KlasseID As String
Get
Return _KlasseID
End Get
Set
_KlasseID = Value
End Set
End Property
Public Property Stueckliste As IList(Of Stueckliste)
Get
Return _Stueckliste
End Get
Set
_Stueckliste = Value
End Set
End Property
Public Property Arbeitsgaenge As IList(Of Arbeitsgaenge)
Get
Return _Arbeitsgaenge
End Get
Set
_Arbeitsgaenge = Value
End Set
End Property
End Class
I'm using a ViewModel where i implement the INotifyChanged Interface:
Public Class ArtikelstammdatenViewModel
Implements INotifyPropertyChanged
Private _ArtikelstammdatenList As List(Of Artikelstammdaten)
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Public Property ArtikelstammdatenList As List(Of Artikelstammdaten)
Get
Return _ArtikelstammdatenList
End Get
Set
_ArtikelstammdatenList = Value
NotifyPropertyChanged("ArtikelstammdatenList")
End Set
End Property
Private Sub NotifyPropertyChanged(ByVal info As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
End Sub
End Class
I'm initializing an object in the MainWindow and fill it with data, which should bind to "EditArtikelstammdaten.xaml"
Dim asd As New Artikelstammdaten
With asd
.Artikel = "VAUBEF0010"
.BezeichnungDE = "Sammelbandantrieb"
.BezeichnungEN = "Collection Belt Drive N50"
.Einheit = "STK"
.MatGrp = "VAU"
.Kostenart = 1500
.Vertriebstext_DE = "Antrieb Umlenkungen"
.Vertriebstext_EN = "Drive, Deflections"
.Stuecklistennummer = "VAUBEF0010"
.Status = "F"
.Klasse = "VTPIMV"
.Mantelflaeche = 1.3
.Gewicht = 120
.KlasseID = "1.2.6.5"
End With
I used "Artikelstammdaten" as DataContext and finally bind each Property to the text Properites.
<Window.DataContext>
<local:Artikelstammdaten></local:Artikelstammdaten>
</Window.DataContext>
<TextBox x:Name="txtItem"
Text="{Binding Artikel}"
Grid.Column="2"
Grid.Row="1"
Margin="0, 5, 0, 8"
FontSize="13"
></TextBox>
What am i missing or doing wrong? Thank you for your time!
My Problem:
I have a Class and a list of other class inside:
Public Class Signal_Type_Read
Private c_signal_count As Integer = 0 ' counter for read signals
Private _items As List(Of Signal_Item)
Private item As New Signal_Item
Sub add_sig()
c_signal_count += 1
items.Add(item)
End Sub
Public Property items() As List(Of Signal_Item)
Get
Return _items
End Get
Set(value As List(Of Signal_Item))
_items = value
End Set
End Property
Function item_counter() As Integer
item_counter = c_signal_count
End Function
Public Sub New()
_items = New List(Of Signal_Item)
End Sub
End Class
Public Class Signal_Item
Private _original_name As String
Public Property Original_name() As String
Get
Return _original_name
End Get
Set(value As String)
_original_name = value
End Set
End Property
'Many other properties
End Class
My problem is when I use in a loop
Public Shared ReadSignals As New Signal_Type_Read
//Part of a Loop to read cells values and store in the variable
ReadSignals.add_sig()
Dim c_index As Integer = ReadSignals.item_counter - 1
ReadSignals.items.item(c_index).Original_name = c_row.Cells(e_Signame).Value
It always changes the "Original_name" Property in all items of my Variable. Where is my error? I want only that oe item is changed.
I found the cause of the problem... I need to create a new instane of item in my ADD_sig() sub
Sub add_sig()
Dim s_item As New Signal_Item
c_signal_count += 1
items.Add(s_item)
End Sub
Despite the many online tutorials, I still do not quite understand how this is supposed to work. I have a radgridview where the user can enter an address per row. I want them to enter the zip code first. The city and state will be supplied from a database matching the zip code. So the radgridview must be updated as soon as the zip code is entered.
It appears that using INotifyProperty is the way to go. I can get the collection to update but cannot get the datagrid to update.
View Model:
Public Class ShipTo
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private Sub NotifyPropertyChanged(ByVal info As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
End Sub
Public Property shipAddressType As String = String.Empty
Public Property orderID As String = String.Empty
Public Property shipName As String = String.Empty
Public Property shipAddr1 As String = String.Empty
Public Property shipAddr2 As String = String.Empty
Private _shipCity As String
Public Property shipCity As String
Get
Return _shipCity
End Get
Set(value As String)
_shipCity = value
NotifyPropertyChanged("shipZip")
End Set
End Property
Private _shipState As String
Public Property shipState As String
Get
Return _shipState
End Get
Set(value As String)
_shipState = value
NotifyPropertyChanged("shipZip")
End Set
End Property
Public Property shipZip As String = String.Empty
Code to find City and State:
Dim shipCS As New ShipTo
shipCS.shipCity = GetCityAndState.CityLookup(CStr(CType(dgShippingAddresses.SelectedItem, ShipTo).shipZip))
shipCS.shipState = GetCityAndState.StateLookup(CStr(CType(dgShippingAddresses.SelectedItem, ShipTo).shipZip))
XAML:
Any help appreciated. Thanks.
I finally got this to work well. For anyone else who is struggling here is my solution:
Private _shipSelected As Boolean
Public Property shipSelected As Boolean
Get
Return _shipSelected
End Get
Set(value As Boolean)
_shipSelected = value
NotifyPropertyChanged("shipSelected")
End Set
End Property
Public Property shipAddressType As String = String.Empty
Public Property orderID As String = String.Empty
Public Property shipName As String = String.Empty
Public Property shipAddr1 As String = String.Empty
Public Property shipAddr2 As String = String.Empty
Private _shipCity As String
Public Property shipCity As String
Get
Return _shipCity
End Get
Set(value As String)
_shipCity = value
NotifyPropertyChanged("shipCity")
End Set
End Property
Private _shipState As String
Public Property shipState As String
Get
Return _shipState
End Get
Set(value As String)
_shipState = value
NotifyPropertyChanged("shipState")
End Set
End Property
Private _shipZip As String
Public Property shipZip As String
Get
Return _shipZip
End Get
Set(value As String)
_shipZip = value
shipCity = GetCityAndState.CityLookup(_shipZip)
shipState = GetCityAndState.StateLookup(_shipZip)
NotifyPropertyChanged("shipZip")
End Set
End Property
Now when the zip is entered in the datagrid, the city and state automatically update.
I have a Class ABC. And i want to write two properties for
it. One i have already mentioned in the code. The other one will be a single dimensional array.
Public Class ABC
Private m_Type As String
Private SomeArray........need to write a property for array which will be of type `int`
Public Property Type() As String
Get
Return m_Type
End Get
Set(ByVal value As String)
m_Type = value
End Set
End Property
End Class
I am not sure how to define a property for array which can be used in a List(Of ABC). The property for array can be a read only array as i will be
hard coding the data for it.
So basically when i do this,
Dim SomeList As New List(Of ABC)
And inside a for loop i need something like this,
SomeList.Item(index).SomeArray......this will give me all the items inside the array
You can declare an array property the same way as you declare a different property type:
Public Class ABC
Private _Type As String
Private _SomeArray As Int32()
Public Property SomeArray As Int32()
Get
Return _SomeArray
End Get
Set(ByVal value As Int32())
_SomeArray = value
End Set
End Property
Public Property Type() As String
Get
Return _Type
End Get
Set(ByVal value As String)
_Type = value
End Set
End Property
End Class
for example, if you want to loop all Integers in one array of the list:
Dim index As Int32 = 0
Dim someList As New List(Of ABC)
For Each i As Int32 In someList(index).SomeArray
Next
If you're not going to be doing anything special in the Gets and Sets, you could simplify your code a little, as in the following (which initializes the read only array to contain the numbers 1, 2, 3 and 4):
Public Class ABC
Public Property Type As String
Public ReadOnly Property SomeArray As Integer() = {1,2,3,4}
End Class
This is my ViewModel Code:
vb:
Public Property Doctor() As Doctor
Get
Return _objDoctor
End Get
Set(ByVal Value As Doctor)
_objDoctor = Value
OnPropertyChanged("Doctor")
End Set
End Property
Public Property AddDate() As Nullable(Of DateTime)
Get
Return _objDoctor.AddDate
End Get
Set(ByVal Value As Nullable(Of DateTime))
_objDoctor.AddDate = Value
OnPropertyChanged("Doctor")
End Set
End Property
Public Property AddUserID() As String
Get
Return _objDoctor.AddUserID
End Get
Set(ByVal Value As String)
_objDoctor.AddUserID = Value
OnPropertyChanged("Doctor")
End Set
End Property
Public Property ChangeDate() As Nullable(Of DateTime)
Get
Return _objDoctor.ChangeDate
End Get
Set(ByVal Value As Nullable(Of DateTime))
_objDoctor.ChangeDate = Value
OnPropertyChanged("Doctor")
End Set
End Property
Public Property ChangeUserID() As String
Get
Return _objDoctor.ChangeUserID
End Get
Set(ByVal Value As String)
_objDoctor.ChangeUserID = Value
OnPropertyChanged("Doctor")
End Set
End Property
Public Property CollaboratingPhysicianID() As Nullable(Of Int64)
Get
Return _objDoctor.CollaboratingPhysicianID
End Get
Set(ByVal Value As Nullable(Of Int64))
_objDoctor.CollaboratingPhysicianID = Value
OnPropertyChanged("Doctor")
End Set
End Property
Public Property CredentialsID() As Int64
Get
Return _objDoctor.CredentialsID
End Get
Set(ByVal Value As Int64)
_objDoctor.CredentialsID = Value
OnPropertyChanged("Doctor")
End Set
End Property
Public Property DisplayName() As String
Get
Return _objDoctor.DisplayName
End Get
Set(ByVal Value As String)
_objDoctor.DisplayName = Value
OnPropertyChanged("Doctor")
End Set
End Property
Public Property DoctorSpecialtyID() As Int64
Get
Return _objDoctor.DoctorSpecialtyID
End Get
Set(ByVal Value As Int64)
_objDoctor.DoctorSpecialtyID = Value
OnPropertyChanged("Doctor")
End Set
End Property
Public Property FirstName() As String
Get
Return _objDoctor.FirstName
End Get
Set(ByVal Value As String)
_objDoctor.FirstName = Value
OnPropertyChanged("Doctor")
End Set
End Property
Public Property ID() As Int64
Get
Return _objDoctor.ID
End Get
Set(ByVal Value As Int64)
_objDoctor.ID = Value
OnPropertyChanged("Doctor")
End Set
End Property
Public Property LastName() As String
Get
Return _objDoctor.LastName
End Get
Set(ByVal Value As String)
_objDoctor.LastName = Value
OnPropertyChanged("LastName")
End Set
End Property
Here is on example of XAML that I could use:
<StackPanel>
<TextBox Text="{Binding LastName}" />
<TextBox Text="{Binding Doctor.LastName}" />
</StackPanel>
I would really prefer to bind to my data as the first textbox is so that I can do validation on changes. (if there is a way to do validation with the second way someone please let me know)
The problem is this:
If I load the doctor in the new constructor of the viewmodel as such:
Public Sub New()
If IsInDesignMode Then
Else
CreateEventSubscriptions()
End If
Me.Doctor = DoctorService.GetDoctor(4839)
End Sub
The Databinding works correctly the first time. But if I try to change the associated doctor with an Event, only the doctor.lastname binding works.
Private Sub onEdit(ByVal d As Doctor)
Me.Doctor = DoctorService.GetDoctor(d)
End Sub
I know I dont need to load a doctor from my service because I am actually passing the doctor...I was trying to see if there was something magical about using a service to fill the propery.
I got the code for my viewmodel properties using Karl Shifflet's XAML PowerToys. Knowing that Karl knows what he is doing I am not sure why I cant get this to work....hopefully it is something silly I am missing.
I believe if you change the doctor during an event, you should also call:
OnPropertyChanged("LastName")
Otherwise I don't think the binding code has any way of knowing the LastName property has been updated.