Hi all, I'm new to WPF and somewhat new to .net after coming from a long background in VB6 and MS Access VBA.
I am currently in the fortunate position where I have been assigned a project on a "deliver as when basis" so I have time to get up to scratch on .net and wpf. My day job is as a pControl developer. This is basically an STP system which takes a
vast amount of business data, combines and transforms it and runs various levels of business logic over the transformed data. My job is basically writing the rules for the business flows.
Users interact with this transformed data in various ways. We also provide substantial support to this and the way currents users log issues is not fit for purpose over the longer term. Hence I have been asked to create a new support
system to capture issues and development requests. I could do this in MS Access in about a month but that's not a challenge and won't provide the quality of solution I am after hence I am going to use VB.Net with WPF.
One of the interesting concepts I have got my head around (just) is databinding. So I basically have learnt how to query the source DB using EF and then bind the WPF form direct to the class using XAML. All good and seems quite clean, however it just
does not seem quite right to me.
What I would prefer to do is to bind the wpf form to an instance of the class rather than to the class itself so that I can use the object through my project. This is an approximation of what I have at the moment, I don't have my actual code to hand but
functionally its does the same thing:
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Namespace comboboxBinding
Public Class ViewModel
Implements INotifyPropertyChanged
Public Sub New()
'EF qry is here
For Each res In qry
Persons.Add(Person)
Next
End Sub
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Public Property Persons As New ObservableCollection(Of Person)
Private _SelectedPerson As Person
Public Property SelectedPerson As Person
Get
Return _SelectedPerson
End Get
Set(ByVal value As Person)
_SelectedPerson = value
OnPropertyChanged("Name")
End Set
End Property
Protected Sub OnPropertyChanged(ByVal name As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
End Sub
End Class
End Namespace
And the xaml:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication4.comboboxBinding"
Title="MainWindow" Height="Auto" Width="525" mc:Ignorable="d"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
d:DesignHeight="300" SizeToContent="Height"><Window.Resources><local:ViewModel x:Key="PersonsViewModel"></local:ViewModel><ObjectDataProvider x:Key="PersonsSource" ObjectInstance="{StaticResource PersonsViewModel}"></ObjectDataProvider></Window.Resources><Grid DataContext="{StaticResource PersonsSource}" Height="150" Width="450"><Grid.RowDefinitions><RowDefinition Height="50" /><RowDefinition Height="50" /><RowDefinition Height="50" /></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition Width="150" /><ColumnDefinition Width="150" /><ColumnDefinition Width="150" /></Grid.ColumnDefinitions><TextBlock Height="15" Text="Bind to person by object:" FontWeight="Bold" /><ComboBox Grid.Column="1" Grid.Row="1" Name="cboBindByObject"
ItemsSource="{Binding Path=Persons}"
DisplayMemberPath="Name"
SelectedItem="{Binding Path=SelectedPerson}"
/></Grid></Window>
What I really want to do is to bind to an instance of ViewModel rather than to ViewModel itself. In other words say I would want to hold all my instantiated ViewModel objects in a separate class and then bind to those rather than bind to the class which
itself does the instantiating!?
In VB6 you could do something like this in a separate module:
Public myViewModel as ViewModel
Set myViewModel = new ViewModel
Which would make myViewModel available to the entire project. This does not seem like good practice in .Net neither can I seem to do it properly.
I maybe worrying too much but it just seems better to bind to an object instance rather than the class itself? Any ideas or am I missing the point.
cheers