Results 1 to 5 of 5

Thread: Databinding Question

  1. #1

    Thread Starter
    Hyperactive Member r0k3t's Avatar
    Join Date
    Dec 2005
    Location
    Cleveland
    Posts
    361

    Databinding Question

    Hi there,

    I have a question about databinding. This is the scenario

    I have this application that I created (just a little test...) and this is the code that sets the listbox itemsource

    Code:
    var q = from e in dc.Employees
                        join c in dc.Contacts on e.ContactID equals c.ContactID
                        select c;
                EmployeesListBox.ItemsSource = q;
    In the XAML I have two stack panels - one on the left with the listbox in it and one the right, it contains just one textbox, what I have done is set the right hand stackpanel's datacontext to the listbox like so...
    Code:
    DataContext="{Binding ElementName=EmployeesListBox, Path=SelectedItem}"
    Then - in the text box I set the path to FirstName - see the following code.
    Code:
    <TextBox Text="{Binding Path=FirstName}" ></TextBox>
    Now - as I click through the names in the listbox the textbox updates nicely. Also - the listbox updates as well cause by default the databinding is going two ways.

    This leads me to my question(s) - two really.

    One - What is the best way to save that data back to the database if I wanted to?

    Second - this is more in-depth... You will notice that I am only selecting one table even though I am doing a join in the query. That is because the object that represents the table implements INotifyPropertyChanged. What happens if I want to do something like the following.
    Code:
    var q = from e in dc.Employees
                        join c in dc.Contacts on e.ContactID equals c.ContactID
                        select new
                        {
                            e.EmployeeID,
                            c.LastName,
                            c.FirstName
                        };
    Now it is broken cause I am assuming the change notification isn't there. What is the best way to handle this? Even if I was OK with selecting everything from the Contacts Table and everything from the Employees table how would I tell it to you "both" of those objects?

    I am new to this stuff so I have a lot to learn so forgive me if this example seems amateurish it is just that there is SOOO much online that it is almost more confusing the more you read...

    Thanks for any advice.
    Anti DUPLO machine!!!

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Databinding Question

    To be honest I am not very familiar with LINQ so your example code does not make much sense to me. However, from what I gather (and I may be wrong) one of your questions is basically how you can databind to something that does not implement INotifyPropertyChanged and have it still update when a change happens? Well its very easy to make your own class implement INotifyPropertyChanged, you simply do something like this:

    vb Code:
    1. Public Class WpfClass : Implements ComponentModel.INotifyPropertyChanged
    2.  
    3.     'define a private field for our property to expose
    4.     Private _SomeField As String
    5.  
    6.     'define our property and make it call our OnPropertyChanged method
    7.     'in the Set block
    8.     Public Property SomeProperty() As String
    9.         Get
    10.             Return _SomeField
    11.         End Get
    12.         Set(ByVal value As String)
    13.             _SomeField = value
    14.             OnPropertyChanged("SomeProperty")
    15.         End Set
    16.     End Property
    17.  
    18.  
    19.     Private Sub OnPropertyChanged(ByVal propertyname As String)
    20.         'Raise the PropertyChanged event and pass in a new instance of the PropertyChangedEventArgs
    21.         'which we pass our property name string to
    22.         RaiseEvent PropertyChanged(Me, New ComponentModel.PropertyChangedEventArgs(propertyname))
    23.     End Sub
    24.  
    25.     'This event must be created for our class to implement the INotifyPropertyChanged interface
    26.     Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
    27.  
    28. End Class

    I just quickly put that together so it might not work straight away but I'm sure it is pretty close if not

    Hope that helps
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Hyperactive Member r0k3t's Avatar
    Join Date
    Dec 2005
    Location
    Cleveland
    Posts
    361

    Re: Databinding Question

    Hi Chris,

    Thanks a lot, I have been looking into this and I found this over at MSDN http://msdn.microsoft.com/en-us/library/aa480226.aspx

    It looks like creating a custom business object is a good way to go but I seem to be having one prolem. I took the Employee and EmployeeList class from the example, didn't have any issue getting them to work however I have had an issue with the mapping.

    This bit of code over at MSDN (my version is below a bit)
    Code:
    <Window
        ...
        xmlns:e="Example"
        DataContext="{StaticResource EmployeeList}"
        >
      <Window.Resources>
        <e:EmployeeList x:Key="EmployeeList" />
        ...
      </Window.Resources>
      ...
    </Window>
    There is something that I am missing there and it is making me nuts... My application is called dataBindingTest and I have two seperate class files, EmployeeList and Employee however I always get and error telling me that it can't find the EmployeeList resource.

    Code:
    $exception	{"Cannot find resource named '{EmployeeList}'. Resource names are case sensitive.  Error at object 'dataBindingTest.Window1' in markup file 'dataBindingTest;component/window1.xaml' Line 5 Position 5."}	System.Exception {System.Windows.Markup.XamlParseException}
    I am obvisoulsy missing something but what? I have tried several combinations when importing the resource but I am stumped. This is what I have.

    Code:
    <Window x:Class="dataBindingTest.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:e="dataBindingTest.EmployeeList"  
        DataContext="{StaticResource EmployeeList}"
        Title="Window1" Height="300" Width="300">
    No matter what I get an error...

    Uhg - It's making me nuts...

    Thanks!
    Anti DUPLO machine!!!

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Databinding Question

    To be honest, I always have problems like this when trying to work with objects in XAML so if I want to databind something I just declare it in code. I still do the actual 'binding' in the XAML of course, its just that my EmployeeList object would be declared at class level in my window's VB code, rather than declared in the XAML. That is probably not much use to you but just thought I would let you know you are not alone with these kind of problems!
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5

    Thread Starter
    Hyperactive Member r0k3t's Avatar
    Join Date
    Dec 2005
    Location
    Cleveland
    Posts
    361

    Re: Databinding Question

    Thanks for your reply. Yes, I agree - Setting the datacontext is usually better done in the code however there are questions of this nature on the 70-502 exam.

    Thanks
    Anti DUPLO machine!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width