Solved

Dependency Property

Posted on 2011-09-06
3
462 Views
Last Modified: 2013-11-12
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

Open in new window



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

Open in new window


Using the user control

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

Open in new window

0
Comment
Question by:Kevin Robinson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 27

Accepted Solution

by:
MikeToole earned 500 total points
ID: 36494494
Your ViewModel is set up to provide data to the View via the Monday property (I'm presuming that the DataContext is set to an instance of the ViewModel somewhere else in your xaml), so why use a Dependency property?
0
 
LVL 25

Expert Comment

by:apeter
ID: 36498830
what is the error/exception here ?
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question