(X) Hide this
    • Login
    • Join
      • Generate New Image
        By clicking 'Register' you accept the terms of use .

Forums

Search in:

Go

dependency property.

  • KevinRobinson
    Kevin Robinson
    0 points
    0 posts
    Sep 6, 2011, 04:03 PM
        Report Abuse
       

    I am try to use a datagrid on a user control an expose the itemsource property via a dependency property.   

    I am using Silverlight 4


    View Model


    Imports System.ServiceModel.DomainServices.Client
    Imports System.Collections.ObjectModel
    Imports GalaSoft.MvvmLight
    Imports GalaSoft.MvvmLight.Command
    Imports GalaSoft.MvvmLight.Messaging


    Public Class TimeSheetsViewModel
        Inherits ViewmodelBase

        Private WithEvents ctx As TimeSheetServiceContext = OPDomainContext.OPDomainContextInstance

    #Region "Properties"

        Private M_monday As New ObservableCollection(Of TIMESHEET)
        Public Property Monday As ObservableCollection(Of TIMESHEET)
            Get
                Return M_monday
            End Get
            Set(value As ObservableCollection(Of TIMESHEET))
                Me.M_monday = value
                RaisePropertyChanged("Monday")
            End Set
        End Property
        Public Property MondayLoading As Boolean = True
     
    #End Region

    #Region "Constructors"
        Public Sub New()
            ctx.Load(Of TIMESHEET)(ctx.GetTimeSheetsMondayQuery(), AddressOf MondayLoaded, Nothing)
        End Sub
    #End Region

    #Region "Private Methdods"
        Private Sub MondayLoaded(args As Loadoperation)
            Try
                Dim _TimeSheets As New ObservableCollection(Of TIMESHEET)

                For Each Timesheet As TIMESHEET In args.Entities
                    _TimeSheets.Add(Timesheet)
                Next

                Me.M_monday = _TimeSheets
          
                MondayLoading = False
            Catch ex As Exception
                ErrorWindow.CreateNew(ex.Message)
            End Try
        End Sub  
    #End Region
    End Class



    User Control



    Partial Public Class Ctrl_TimeSheetDataGrid
        Inherits UserControl

        Public Sub New()
            InitializeComponent()
        End Sub

        Public Property TimeSheetItemsSource() As ObservableCollection(Of TIMESHEET)
            Get
                Return DirectCast(GetValue(ItemsSourceProperty), ObservableCollection(Of TIMESHEET))
            End Get
            Set(value As ObservableCollection(Of TIMESHEET))
                SetValue(ItemsSourceProperty, value)
            End Set
        End Property

        Dim ItemsSourceProperty As DependencyProperty = DependencyProperty.Register("TimeSheetItemsSource", GetType(ObservableCollection(Of TIMESHEET)), GetType(Ctrl_TimeSheetDataGrid), New PropertyMetadata(New PropertyChangedCallback(AddressOf onItemsSourcePropertyChanged)))

        Private Sub onItemsSourcePropertyChanged(sender As Object, dp As DependencyPropertyChangedEventArgs)
            Me.dgMonday.ItemsSource = dp.NewValue
        End Sub
    End Class


    Using the user control


    <ds:Ctrl_TimeSheetDataGrid x:Name="dgMonday"  TimeSheetItemsSource="{Binding Monday}"/>
    Reply

All Posts

  • Enrai
    Martin Mihaylov
    1092 points
    6 posts
    Sep 12, 2011, 06:14 PM
            Report Abuse
    Hi, Kevin! Is the code, which you have posted here, not working? And if yes, what is the exception or the wrong behavior you get?

    Anyway, I can suggest you to use a custom control instead of user control in case of data-binding-scenarios, as it will spare you a lot of problems. Unless you have a specific reason to use a user control?

    Greets,
    Martin
    Reply
  • ppopadiyn
    Pencho Popadiyn
    591 points
    17 posts
    Sep 13, 2011, 06:25 PM
            Report Abuse
    Why you need to define the DataSource property in the ViewModel as a Dependency Property ? Just use a regular property, and everything should work perfect , I think :)
    Reply
Page  
  • 1

To reply to this thread you must Log In to your account


Thread


Tags: 
  • Asked
    473 days ago
  • Viewed
    920
  • Last Activity

Related Threads


Shortcuts