none
Can't bind EF to WPF Listbox RRS feed

  • Question

  • Hi I've used the "How to get started with entity frameworks and wpf" example found here

    http://msdn.microsoft.com/en-us/vstudio/dd776537.aspx

    It works fine in VS2008 but doesn't work in VS2012 ? I.e. the displaymemberpath is showing the entity name rather than the value i.e. "WpfApplication1.tb_access" tb_access is the table and entity name.

    My target development environment is 2012 and I'm not experienced enough to know whats going on.

    I've tried both "Code Generation" = Default and None and have set a debug point in both 2008 and 2012 on the displaymemberpath get property that I'm trying to bind, its called "acc_company", in 2008 the debug point is always triggered as you would expect in 2012 the get for property acc_company is never triggered?

    What can I do to resolve this?

    Tuesday, August 20, 2013 4:58 PM

Answers

  • Can you please show your XAML and code behind for the VS 2012 project.

    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.


    Tuesday, August 20, 2013 7:43 PM

All replies

  • Can you please show your XAML and code behind for the VS 2012 project.

    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.


    Tuesday, August 20, 2013 7:43 PM
  • Hi Thanks for reply. My code is as follows:-

    XAML:

    <Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
        <Grid>
            <ListView x:Name="Listbox1">
                <ListView.View>
                    <GridView>
                        <GridViewColumn/>
                    </GridView>
                </ListView.View>
            </ListView>
        </Grid>
    </Window>

    Code Behind:

        Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
    
            Dim db = New TEDEntities
    
            Dim query = From b In db.tb_Access
                        Take 100
                        Order By b.acc_accno
                        Select b
    
            Me.Listbox1.ItemsSource = query.ToList()
            Me.Listbox1.DisplayMemberPath = "acc_company"
    
        End Sub

    The result in VS2012 is as follows:

    WpfApplication1.tb_Access
    WpfApplication1.tb_Access
    WpfApplication1.tb_Access
    WpfApplication1.tb_Access

    The result in VS2008 is a list of company names as expected.

    The getter defined automatically for the Entity for acc_company is as follows:

    2012:

        <EdmScalarPropertyAttribute(EntityKeyProperty:=false, IsNullable:=true)>
        <DataMemberAttribute()>
        Public Property acc_company() As Global.System.String
            Get
                Return _acc_company
            End Get
            Set
                Onacc_companyChanging(value)
                ReportPropertyChanging("acc_company")
                _acc_company = StructuralObject.SetValidValue(value, true, "acc_company")
                ReportPropertyChanged("acc_company")
                Onacc_companyChanged()
            End Set
        End Property

    And the same getter defined automatically in 2008 is:

        '''<summary>
        '''There are no comments for property acc_company in the schema.
        '''</summary>
        <Global.System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(),  _
         Global.System.Runtime.Serialization.DataMemberAttribute(),  _
         Global.System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")>  _
        Public Property acc_company() As String
            Get
                Return Me._acc_company
            End Get
            Set
                Me.Onacc_companyChanging(value)
                Me.ReportPropertyChanging("acc_company")
                Me._acc_company = Global.System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value, true)
                Me.ReportPropertyChanged("acc_company")
                Me.Onacc_companyChanged
            End Set
        End Property

    The entity in both cases is linked to the same SQL Server 2008 database on the local machine.

    What I don't get is that the getter fires OK in 2008 and gets ignored in 2012??

    Hope you can help. Got a lot more learning to do!

    Thanks & Regards

    Ted

    Wednesday, August 21, 2013 8:14 AM
  • Ahhh. Found the issue! How embarrasing. Was using a differemt control in each version ! Never mind, thanks for your help. At least it got me to go through the code at a deeper level and increased my understanding :)
    Wednesday, August 21, 2013 9:11 AM
  • Glad that you were able to locate the issue.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, August 21, 2013 1:04 PM