RadGridView Cell

8 posts, 0 answers
  1. sam Aryan
    sam Aryan avatar
    24 posts
    Member since:
    Feb 2010

    Posted 31 Aug 2010 Link to this post

    Hi there,

    can somebody please tell me how I can disable a row or cell dynamically in a grid view in MVVM?

    Cheers,

    Farzin
  2. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 31 Aug 2010 Link to this post

    Hi,

     You can use CellStyleSelector and RowStyleSelector to set IsEnabled property depending on your condition. 

    Regards,
    Vlad
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft banner
  4. sam Aryan
    sam Aryan avatar
    24 posts
    Member since:
    Feb 2010

    Posted 31 Aug 2010 Link to this post

    Hi Vlad,

    Can you please provide me with a sample solution. I have been trying to implement it by myself but so far without success.

    Cheers,

    Farzin
  5. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 02 Sep 2010 Link to this post

    Hi,

     The sample code can be found in both examples I've posted in my previous reply. What kind if problems you have? Can you post what you've tried (your code)?

    Best wishes,
    Vlad
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. Jerry T.
    Jerry T. avatar
    118 posts
    Member since:
    Jul 2012

    Posted 01 Jul 2011 Link to this post

    That sample solution is not a solution. It's some code snippets.

    It doesn't show the code for the converter.

    It also doesn't follow MVVM.

    Can Telerik post FULL solutions using MVVM for samples when people request them? It would REALLY help out those of us new to the MVVM method.
  7. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 04 Jul 2011 Link to this post

    Hello,

     Style and Template selectors and IValueConverters are actually pure MVVM. You may need to check your local version of our demos for more info.

    Best wishes,
    Vlad
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  8. Jerry T.
    Jerry T. avatar
    118 posts
    Member since:
    Jul 2012

    Posted 05 Jul 2011 Link to this post

    Hello.

    I have the Demo code (for Q1 2011 and the Q2 2011 as well) and there's nothing in there that follows the full MVVM methodology.

    This is how I have it working as of now, though (the disabling/enabling of cells is in the View's Code Behind, along with code for binding a ComboBox to a different data source when the GridView is in Edit mode):

    Web App: DMCpro.Web.App

    Domain Service:
    Imports System.ServiceModel.DomainServices.Hosting
    Imports System.ServiceModel.DomainServices.Server
     
    Namespace Services
     
        <EnableClientAccess()>
        <RequiresAuthentication()>
        Public Class CostingSheetDomainService
            Inherits DomainService
     
    #Region "Public Methods"
     
            Public Function GetDefaultCostingSheets() As IEnumerable(Of Model.DefaultCostingSheet)
     
                Me.EnsurePrincipal()
     
                Return (
                    From i In BAL.GetCostingSheetsDefaultCostingSheets()
                    Select New Model.DefaultCostingSheet() With {
                        .DefaultCostingSheetId = i.DefaultCostingSheetId,
                        .DefaultCostingSheetName = i.DefaultCostingSheetName,
                        .ServiceType = i.ServiceType,
                        .OfficeName = i.OfficeName,
                        .ProgramLocation = i.ProgramLocation,
                        .Year = i.Year,
                        .Pax = i.Pax,
                        .DateCreated = i.DateCreated,
                        .DateUpdated = i.DateUpdated,
                        .IsActive = i.IsActive
                    }
                )
     
            End Function
     
            Public Function GetServiceCostingSheet(lineNumber As Integer, status As String) As IEnumerable(Of Model.ServiceCostingSheet)
                Me.EnsurePrincipal()
     
                Return (
                    From i In BAL.GetProgramsCostingSheetsForService(lineNumber, status)
                    Select New Model.ServiceCostingSheet() With {
                        .fldCostingSheetID = i.fldCostingSheetID,
                        .fldCostingSheetHeaderID = i.fldCostingSheetHeaderID,
                        .fldServiceVenueItemID = i.fldServiceVenueItemID,
                        .fldCategory = i.fldCategory,
                        .CostCatAbv = i.CostCatAbv,
                        .fldCostingItem = i.fldCostingItem,
                        .fldCostPerUnit = i.fldCostPerUnit,
                        .fldHours = i.fldHours,
                        .fldGratuity = i.fldGratuity,
                        .fldUnits = i.fldUnits,
                        .fldTax = i.fldTax,
                        .fldTaxable = i.fldTaxable,
                        .fldCost = i.fldCost,
                        .GP = i.GP,
                        .fldUnitRev = i.fldUnitRev,
                        .PricingOption = i.PricingOption,
                        .fldRevenue = i.fldRevenue,
                        .fldRevPerPerson = i.fldRevPerPerson,
                        .fldCommOpt = i.fldCommOpt,
                        .CommOption = i.CommOption,
                        .BillType = i.BillType,
                        .fldGroupType = i.fldGroupType,
                        .fldSortOrder = i.fldSortOrder,
                        .VendorName = i.VendorName,
                        .fldPricingOptionID = i.fldPricingOptionID,
                        .fldBilltype = i.fldBilltype,
                        .fldTax1 = i.fldTax1,
                        .fldTax2 = i.fldTax2,
                        .fldVendorID = i.fldVendorID,
                        .fldAmtVendorPd = i.fldAmtVendorPd,
                        .Status = i.Status,
                        .fldCommClient = i.fldCommClient,
                        .fldCommHotel = i.fldCommHotel,
                        .fldClientCommRate = i.fldClientCommRate,
                        .fldHotelCommRate = i.fldHotelCommRate
                    }
                )
            End Function
     
            Public Function GetCostingSheetPricingOptions() As IEnumerable(Of Model.CostingSheetPricingOptions)
                Me.EnsurePrincipal()
     
                Return (
                    From i In BAL.GetCostingSheetPricingOptions()
                    Select New Model.CostingSheetPricingOptions() With {
                        .PricingOptionID = i.PricingOptionID,
                        .PricingOption = i.PricingOption
                    }
                )
            End Function
     
            Public Function GetCostingSheetBillTypes() As IEnumerable(Of Model.CostingSheetBillTypes)
                Me.EnsurePrincipal()
     
                Return (
                    From i In BAL.GetCostingBillTypes()
                    Select New Model.ServiceCostingSheet() With {
                        .fldBilltype = i.BillTypeID,
                        .BillType = i.BillType
                    }
                )
            End Function
     
            Public Function GetCostingSheetCommissionOptions() As IEnumerable(Of Model.CostingSheetCommissionOptions)
                Me.EnsurePrincipal()
     
                Return (
                    From i In BAL.GetCostingCommissionOptions()
                    Select New Model.ServiceCostingSheet() With {
                        .fldCommOpt = i.CommOptionID,
                        .CommOption = i.CommOption
                    }
                )
            End Function
     
    #End Region
     
    #Region "Private Methods"
     
            Private Sub EnsurePrincipal()
                Threading.Thread.CurrentPrincipal = ServiceContext.User
            End Sub
     
            Protected Overrides Function Count(Of T)(query As System.Linq.IQueryable(Of T)) As Int32
                Return query.Count()
            End Function
     
    #End Region
     
        End Class
     
    End Namespace


    Silverlight Project Name: DMCpro.Silverlight.ServiceCostingSheet

    VIEW - XAML
    <UserControl x:Class="DMCpro.Silverlight.ServiceCostingSheet.View.ServiceCostingSheetView"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400">
         
        <UserControl.Resources>
            <Style x:Key="CostingSheetLabelBorder" TargetType="Border">
                <Setter Property="CornerRadius" Value="10" />
                <Setter Property="Background" Value="#FFDEDEDE" />
                <Setter Property="Margin" Value="0,0,5,0" />
                <Setter Property="Grid.Column" Value="0" />
            </Style>
             
            <Style x:Key="CostingSheetLabel" TargetType="TextBlock">
                <Setter Property="Foreground" Value="#FF14517B" />
                <Setter Property="Margin" Value="10,3,0,0" />
            </Style>
        </UserControl.Resources>
     
        <Grid x:Name="LayoutRoot" Background="White">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
             
            <Grid.Effect>
                <DropShadowEffect Color="Gray" Direction="0" ShadowDepth="0" BlurRadius="20" Opacity="1" />
            </Grid.Effect>
     
            <telerik:RadGridView EnableColumnVirtualization="False" x:Name="rgvProgramServiceCostingSheet" Grid.Row="0" CanUserFreezeColumns="False" AutoGenerateColumns="False" IsFilteringAllowed="False"
                ItemsSource="{Binding ServiceCostingSheets}" EditTriggers="CellClick"
                RowLoaded="rgvProgramServiceCostingSheet_RowLoaded" Loaded="rgvProgramServiceCostingSheet_Loaded"
                AllowDrop="False" ScrollMode="RealTime" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" ShowGroupPanel="False" RowDetailsVisibilityMode="Visible">
                <telerik:RadGridView.Columns>
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Model.VendorName}" Header="Vendor" Width="150" EditTriggers="CellClick" UniqueName="VendorName" >
                        <telerik:GridViewDataColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Model.VendorName}" />
                            </DataTemplate>
                        </telerik:GridViewDataColumn.CellTemplate>
                        <telerik:GridViewDataColumn.CellEditTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding Model.VendorName, Mode=OneWay}"  />
                            </DataTemplate>
                        </telerik:GridViewDataColumn.CellEditTemplate>
                    </telerik:GridViewDataColumn>
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Model.fldCostingItem}" Header="Costing Item" Width="200" EditTriggers="CellClick">
                        <telerik:GridViewDataColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Model.fldCostingItem}" />
                            </DataTemplate>
                        </telerik:GridViewDataColumn.CellTemplate>
                        <telerik:GridViewDataColumn.CellEditTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding Model.fldCostingItem, Mode=TwoWay}"  />
                            </DataTemplate>
                        </telerik:GridViewDataColumn.CellEditTemplate>
                    </telerik:GridViewDataColumn>
                    <telerik:GridViewMaskedTextBoxColumn DataMemberBinding="{Binding Model.fldCostPerUnit}" Mask="c2" DataFormatString="{}{0:C2}" Header="Unit Cost" Width="95" HeaderTextAlignment="Right" TextAlignment="Right" UniqueName="fldCostPerUnit" />
                    <telerik:GridViewMaskedTextBoxColumn DataMemberBinding="{Binding Model.fldUnits}" Mask="n0" Header="Units" Width="45" HeaderTextAlignment="Right" TextAlignment="Right" />
                    <telerik:GridViewMaskedTextBoxColumn DataMemberBinding="{Binding Model.fldHours}" Mask="n0" Header="Hours" Width="45" HeaderTextAlignment="Right" TextAlignment="Right" />
                    <telerik:GridViewMaskedTextBoxColumn DataMemberBinding="{Binding Model.fldGratuity}" Mask="c2" DataFormatString="{}{0:C2}" Header="Gratuity" Width="95" HeaderTextAlignment="Right" TextAlignment="Right" />
                    <telerik:GridViewMaskedTextBoxColumn DataMemberBinding="{Binding Model.fldTax}" Mask="p3" DataFormatString="{}{0:P3}" Header="Tax" Width="95" HeaderTextAlignment="Right" TextAlignment="Right" />
                    <telerik:GridViewCheckBoxColumn DataMemberBinding="{Binding Model.fldTaxable}" Header="Tax?" Width="45" EditTriggers="CellClick" />
                    <telerik:GridViewMaskedTextBoxColumn DataMemberBinding="{Binding Model.fldCost}" Mask="c" DataFormatString="{}{0:C2}" Header="Cost" Width="95" HeaderTextAlignment="Right" TextAlignment="Right" />
                    <telerik:GridViewMaskedTextBoxColumn DataMemberBinding="{Binding Model.GP}" Mask="p0" DataFormatString="{}{0:P0}" Header="GP%" Width="45" HeaderTextAlignment="Right" TextAlignment="Right" />
                    <telerik:GridViewMaskedTextBoxColumn DataMemberBinding="{Binding Model.fldUnitRev}" Mask="c2" DataFormatString="{}{0:C2}" Header="Unit Rev" Width="95" HeaderTextAlignment="Right" TextAlignment="Right" />
                    <telerik:GridViewComboBoxColumn x:Name="rcbPricingOption" DataMemberBinding="{Binding Model.PricingOption}" Header="Pricing" Width="100">
                        <telerik:GridViewComboBoxColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Model.PricingOption}" />
                            </DataTemplate>
                        </telerik:GridViewComboBoxColumn.CellTemplate>
                        <telerik:GridViewComboBoxColumn.CellEditTemplate>
                            <DataTemplate>
                                <ComboBox x:Name="cbPricingOption"  Loaded="cbPricingOption_Loaded" />
                            </DataTemplate>
                        </telerik:GridViewComboBoxColumn.CellEditTemplate>
                    </telerik:GridViewComboBoxColumn>
                    <telerik:GridViewMaskedTextBoxColumn DataMemberBinding="{Binding Model.fldRevenue}" Mask="c2" DataFormatString="{}{0:C2}" Header="Revenue" Width="95" HeaderTextAlignment="Right" TextAlignment="Right" />
                    <telerik:GridViewMaskedTextBoxColumn DataMemberBinding="{Binding Model.fldRevPerPerson}" Mask="c2" DataFormatString="{}{0:C2}" Header="Rev PP" Width="95" HeaderTextAlignment="Right" TextAlignment="Right" />
                    <telerik:GridViewComboBoxColumn x:Name="rcbCommOption" DataMemberBinding="{Binding Model.CommOption}" Header="Comm" Width="100">
                        <telerik:GridViewComboBoxColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Model.CommOption}" />
                            </DataTemplate>
                        </telerik:GridViewComboBoxColumn.CellTemplate>
                        <telerik:GridViewComboBoxColumn.CellEditTemplate>
                            <DataTemplate>
                                <ComboBox DisplayMemberPath="CommOption" SelectedValuePath="CommOptionID" SelectedValue="{Binding Model.fldCommOpt, Mode=TwoWay}" ItemsSource="{Binding CommissionOptions, Source={StaticResource CommissionOptionsList}}" />
                            </DataTemplate>
                        </telerik:GridViewComboBoxColumn.CellEditTemplate>
                    </telerik:GridViewComboBoxColumn>
                    <telerik:GridViewComboBoxColumn x:Name="rcbBillType" DataMemberBinding="{Binding Model.BillType}" Header="Bill Type" Width="75">
                        <telerik:GridViewComboBoxColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Model.BillType}" />
                            </DataTemplate>
                        </telerik:GridViewComboBoxColumn.CellTemplate>
                        <telerik:GridViewComboBoxColumn.CellEditTemplate>
                            <DataTemplate>
                                <ComboBox DisplayMemberPath="BillType" SelectedValuePath="BillTypeID" SelectedValue="{Binding Model.fldBilltype, Mode=TwoWay}" ItemsSource="{Binding BillTypes, Source={StaticResource BillTypesList}}" />
                            </DataTemplate>
                        </telerik:GridViewComboBoxColumn.CellEditTemplate>
                    </telerik:GridViewComboBoxColumn>
                    <telerik:GridViewMaskedTextBoxColumn DataMemberBinding="{Binding Model.fldGroupType}" Header="Group" Width="45" HeaderTextAlignment="Right" TextAlignment="Right" />
                    <telerik:GridViewMaskedTextBoxColumn DataMemberBinding="{Binding Model.fldSortOrder}" Header="Sort" Width="45" HeaderTextAlignment="Right" TextAlignment="Right" />
                </telerik:RadGridView.Columns>
            </telerik:RadGridView>
             
            <StackPanel Grid.Row="1" Orientation="Horizontal">
                <Border Style="{StaticResource CostingSheetLabelBorder}">
                    <TextBlock x:Name="TaxLabel" Style="{StaticResource CostingSheetLabel}"/>
                </Border>
                <Border Style="{StaticResource CostingSheetLabelBorder}">
                    <TextBlock x:Name="CostLabel" Style="{StaticResource CostingSheetLabel}"/>
                </Border>
                <Border Style="{StaticResource CostingSheetLabelBorder}">
                    <TextBlock x:Name="RevenueLabel" Style="{StaticResource CostingSheetLabel}"/>
                </Border>
                <Border Style="{StaticResource CostingSheetLabelBorder}">
                    <TextBlock x:Name="RevPerPersonLabel" Style="{StaticResource CostingSheetLabel}"/>
                </Border>
                <Border Style="{StaticResource CostingSheetLabelBorder}">
                    <TextBlock x:Name="GPLabel" Style="{StaticResource CostingSheetLabel}"/>
                </Border>
            </StackPanel>
     
        </Grid>
     
    </UserControl>


    VIEW - Code Behind
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Windows;
    using System.Windows.Browser;
    using System.Windows.Controls;
    using Telerik.Windows.Controls;
    using Telerik.Windows.Controls.GridView;
    using DMCpro.Silverlight.ServiceCostingSheet.Model;
    using DMCpro.Silverlight.ServiceCostingSheet.ViewModel;
    using DMCpro.Silverlight.CostingSheetPricingOptions.ViewModel;
     
    namespace DMCpro.Silverlight.ServiceCostingSheet.View
    {
        public partial class ServiceCostingSheetView : UserControl
        {
            private decimal m_Tax = 0;
            private decimal m_Cost = 0;
            private decimal m_Revenue = 0;
            private decimal m_RevPerPerson = 0;
            private decimal m_CommTTL = 0;
            private decimal m_HotelCommRate = 0;
            private decimal m_HotelCommTTL = 0;
            private decimal m_OtherComm = 0;
            private decimal m_OtherCommTTL = 0;
            private string m_NewAssignedTo = string.Empty;
            private string m_CostingSheetType = string.Empty;
            private int m_newPricingOption = 0;
            private int m_newCommOption = 0;
            private int m_newBillType = 0;
     
            #region Constructor
     
            public ServiceCostingSheetView()
            {
                InitializeComponent();
     
                int lineNumber = Convert.ToInt32(HtmlPage.Document.GetElementById("hiddenLineNumber").GetProperty("Value"));
                string status = HtmlPage.Document.GetElementById("hiddenStatus").GetProperty("Value").ToString();
     
                this.DataContext = new MainViewModel(new ServiceCostingSheetServiceProxy(lineNumber, status));
            }
     
            #endregion
     
     
            private void rgvProgramServiceCostingSheet_RowLoaded(object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e)
            {
                if ((e.Row is GridViewRow)) {
                    GridViewRow gvr = (GridViewRow)e.Row;
                    var cells = (e.Row as GridViewRow).ChildrenOfType<GridViewCell>();
                    var dataItem = (e.DataElement as ServiceCostingSheetViewModel).Model;
     
                    decimal Tax1 = dataItem.fldTax1;
                    decimal Tax2 = dataItem.fldTax2;
     
                    m_Tax += Tax1 + Tax2;
                    m_Cost += dataItem.fldCost;
                    m_Revenue += dataItem.fldRevenue;
                    m_RevPerPerson += dataItem.fldRevPerPerson;
                    m_CommTTL += dataItem.fldCommHotel + dataItem.fldCommClient;
     
                    switch ((string)cells[11].Value)
                    {
                        case "TTLRev":
                            //Total Revenue
                            cells[9].IsEnabled = false;
                            cells[10].IsEnabled = false;
                            cells[12].IsEnabled = true;
     
                            break;
                        case "TTLRevLock":
                            //Total Revenue Locked
                            cells[9].IsEnabled = false;
                            cells[10].IsEnabled = false;
                            cells[12].IsEnabled = true;
     
                            break;
                        case "UnitRev":
                            //Unit Revenue
                            cells[9].IsEnabled = false;
                            cells[10].IsEnabled = true;
                            cells[12].IsEnabled = false;
     
                            break;
                        default:
                            //GP% or other
                            cells[9].IsEnabled = true;
                            cells[10].IsEnabled = false;
                            cells[12].IsEnabled = false;
                            break;
                    }
     
                    // If Vendor has made payments, disable changes
                    if (dataItem.fldAmtVendorPd != 0)
                    {
                        cells[0].IsEnabled = false;
                    }
     
                    TaxLabel.Text = "Tax: " + m_Tax.ToString();
                    CostLabel.Text = "Cost: " + m_Cost.ToString();
                    RevenueLabel.Text = "Rev: " + m_Revenue.ToString();
                    RevPerPersonLabel.Text = "Rev PP: " + m_RevPerPerson.ToString();
                    GPLabel.Text = "GP Sub: " + (m_Revenue - m_Cost).ToString();
                }
            }
     
            private void rgvProgramServiceCostingSheet_Loaded(object sender, System.EventArgs e)
            {
            }
     
            private void cbPricingOption_Loaded(object sender, RoutedEventArgs e)
            {
                ComboBox cbPricingOption = (ComboBox)sender;
     
                cbPricingOption.ItemsSource = (this.DataContext as MainViewModel).CostingSheetPricingOptions;
                cbPricingOption.SelectedValuePath = "Model.PricingOptionID";
                cbPricingOption.DisplayMemberPath = "Model.PricingOption";
                cbPricingOption.SelectedValue = (rgvProgramServiceCostingSheet.Items.CurrentEditItem as ServiceCostingSheetViewModel).Model.fldPricingOptionID;
            }
        }
    }

    VIEWMODEL - MainViewModel.cs
    using System;
    using System.ComponentModel;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using DMCpro.Silverlight.MvvmHelpers;
    using DMCpro.Silverlight.ServiceCostingSheet.Model;
    using DMCpro.Silverlight.ServiceCostingSheet.ViewModel;
    using DMCpro.Silverlight.CostingSheetPricingOptions.ViewModel;
    using DMCpro.Web.App.Model;
     
    namespace DMCpro.Silverlight.ServiceCostingSheet.ViewModel
    {
        public class MainViewModel : ViewModelBase
        {
            public ObservableCollection<ServiceCostingSheetViewModel> ServiceCostingSheets { get; private set; }
            public List<CostingSheetPricingOptionsViewModel> CostingSheetPricingOptions
            {
                get
                {
                    return _CostingSheetPricingOptions;
                }
     
              set
              {
                  _CostingSheetPricingOptions = value;
              }
            }
     
            private List<CostingSheetPricingOptionsViewModel> _CostingSheetPricingOptions;
     
            /// <summary>
            /// The <see cref="ErrorMessage" /> property's name.
            /// </summary>
            public const string ErrorMessagePropertyName = "ErrorMessage";
     
            private string _errorMessage = string.Empty;
     
            /// <summary>
            /// Gets the ErrorMessage property.
            /// Changes to that property's value raise the PropertyChanged event.
            /// </summary>
            public string ErrorMessage
            {
                get
                {
                    return _errorMessage;
                }
                set
                {
                    if (_errorMessage == value) return;
                    _errorMessage = value;
                    RaisePropertyChanged(ErrorMessagePropertyName);
                }
            }
     
            private IServiceCostingSheetServiceProxy _service;
     
            public RelayCommand SaveCommand { get; private set; }
     
            public MainViewModel(IServiceCostingSheetServiceProxy service)
            {
                this.ServiceCostingSheets = new ObservableCollection<ServiceCostingSheetViewModel>();
                this.CostingSheetPricingOptions = new List<CostingSheetPricingOptionsViewModel>();
                _service = service;
                _service.GetServiceCostingSheet(OnGetServiceCostingSheetCompleted);
                _service.GetCostingSheetPricingOptions(OnGetCostingSheetPricingOptionsCompleted);
     
                //SaveCommand = new RelayCommand(SaveCustomers);
     
            }
     
            private void OnGetServiceCostingSheetCompleted(IEnumerable<DMCpro.Web.App.Model.ServiceCostingSheet> result, Exception ex)
            {
                this.ServiceCostingSheets.Clear();
     
                if (ex != null)
                {
                    ErrorMessage = ex.Message;
                    return;
                }
     
                if (result == null) return;
     
                foreach (var i in result) this.ServiceCostingSheets.Add(new ServiceCostingSheetViewModel(i));
            }
     
            private void OnGetCostingSheetPricingOptionsCompleted(IEnumerable<DMCpro.Web.App.Model.CostingSheetPricingOptions> result, Exception ex)
            {
                if (ex != null)
                {
                    ErrorMessage = ex.Message;
                    return;
                }
     
                if (result == null) return;
     
                foreach (var i in result) this.CostingSheetPricingOptions.Add(new CostingSheetPricingOptionsViewModel(i));
            }
        }
    }

    VIEWMODEL - ViewModel for GridView control
    using System;
    using System.Windows;
    using DMCpro.Silverlight.MvvmHelpers;
    using DMCpro.Web.App.Model;
     
    namespace DMCpro.Silverlight.ServiceCostingSheet.ViewModel
    {
        public class ServiceCostingSheetViewModel : ViewModelBase
        {
            public DMCpro.Web.App.Model.ServiceCostingSheet Model { get; private set; }
     
            /// <summary>
            /// The <see cref="DirtyVisibility" /> property's name.
            /// </summary>
            public const string DirtyVisibilityPropertyName = "DirtyVisibility";
     
            private Visibility _dirty = Visibility.Collapsed;
     
            /// <summary>
            /// Gets the DirtyVisibility property.
            /// Changes to that property's value raise the PropertyChanged event.
            /// </summary>
            public Visibility DirtyVisibility
            {
                get
                {
                    return _dirty;
                }
                set
                {
                    if (_dirty == value) return;
                    _dirty = value;
                    RaisePropertyChanged(DirtyVisibilityPropertyName);
                }
            }
     
            public ServiceCostingSheetViewModel(DMCpro.Web.App.Model.ServiceCostingSheet model)
            {
                Model = model;
                model.PropertyChanged += (s, e) => { DirtyVisibility = Visibility.Visible; };
            }
     
        }
    }


    VIEWMODEL - GridView Edit mode RadComboBox
    using System;
    using System.Windows;
    using DMCpro.Silverlight.MvvmHelpers;
    using DMCpro.Web.App.Model;
     
    namespace DMCpro.Silverlight.CostingSheetPricingOptions.ViewModel
    {
        public class CostingSheetPricingOptionsViewModel : ViewModelBase
        {
            public DMCpro.Web.App.Model.CostingSheetPricingOptions Model { get; private set; }
     
            /// <summary>
            /// The <see cref="DirtyVisibility" /> property's name.
            /// </summary>
            public const string DirtyVisibilityPropertyName = "DirtyVisibility";
     
            private Visibility _dirty = Visibility.Collapsed;
     
            /// <summary>
            /// Gets the DirtyVisibility property.
            /// Changes to that property's value raise the PropertyChanged event.
            /// </summary>
            public Visibility DirtyVisibility
            {
                get
                {
                    return _dirty;
                }
                set
                {
                    if (_dirty == value) return;
                    _dirty = value;
                    RaisePropertyChanged(DirtyVisibilityPropertyName);
                }
            }
     
            public CostingSheetPricingOptionsViewModel(DMCpro.Web.App.Model.CostingSheetPricingOptions model)
            {
                Model = model;
                model.PropertyChanged += (s, e) => { DirtyVisibility = Visibility.Visible; };
            }
     
        }
    }


    MODEL - Proxy
    using System;
    using System.Collections.Generic;
    using model = DMCpro.Web.App.Model;
    using DMCpro.Web.App.Services;
    using System.ServiceModel.DomainServices.Client;
     
    namespace DMCpro.Silverlight.ServiceCostingSheet.Model
    {
        public class ServiceCostingSheetServiceProxy : IServiceCostingSheetServiceProxy
        {
            private CostingSheetDomainContext _context;
     
            private int _lineNumber;
            private string _status;
     
            public ServiceCostingSheetServiceProxy(int lineNumber, string status)
            {
                _lineNumber = lineNumber;
                _status = status;
            }
             
            public void GetServiceCostingSheet(Action<IEnumerable<model.ServiceCostingSheet>, Exception> callback)
            {
                this.EnsureContext();
     
                _context.Load
                (
                    _context.GetServiceCostingSheetQuery(_lineNumber,_status),
                    (e) => { callback(!e.HasError ? e.Entities : null, e.HasError ? e.Error : null); },
                    null
                );
            }
     
            public void GetCostingSheetPricingOptions(Action<IEnumerable<model.CostingSheetPricingOptions>, Exception> callback)
            {
                this.EnsureContext();
     
                _context.Load
                (
                    _context.GetCostingSheetPricingOptionsQuery(),
                    (e) => { callback(!e.HasError ? e.Entities : null, e.HasError ? e.Error : null); },
                    null
                );
            }
     
            public void GetCostingSheetBillTypes(Action<IEnumerable<model.CostingSheetBillTypes>, Exception> callback)
            {
                this.EnsureContext();
     
                _context.Load
                (
                    _context.GetCostingSheetBillTypesQuery(),
                    (e) => { callback(!e.HasError ? e.Entities : null, e.HasError ? e.Error : null); },
                    null
                );
            }
     
            public void GetCostingSheetCommissionOptions(Action<IEnumerable<model.CostingSheetCommissionOptions>, Exception> callback)
            {
                this.EnsureContext();
     
                _context.Load
                (
                    _context.GetCostingSheetCommissionOptionsQuery(),
                    (e) => { callback(!e.HasError ? e.Entities : null, e.HasError ? e.Error : null); },
                    null
                );
            }
     
            private void EnsureContext()
            {
                if (_context == null) _context = new CostingSheetDomainContext();
            }
        }
    }


    MODEL - Interface for Proxy
    using System;
    using System.Collections.Generic;
    using DMCpro.Web.App.Model;
     
    namespace DMCpro.Silverlight.ServiceCostingSheet.Model
    {
        public interface IServiceCostingSheetServiceProxy
        {
            void GetServiceCostingSheet(Action<IEnumerable<DMCpro.Web.App.Model.ServiceCostingSheet>, Exception> callback);
     
            void GetCostingSheetPricingOptions(Action<IEnumerable<DMCpro.Web.App.Model.CostingSheetPricingOptions>, Exception> callback);
     
            void GetCostingSheetBillTypes(Action<IEnumerable<DMCpro.Web.App.Model.CostingSheetBillTypes>, Exception> callback);
     
            void GetCostingSheetCommissionOptions(Action<IEnumerable<DMCpro.Web.App.Model.CostingSheetCommissionOptions>, Exception> callback);
        }
    }


    MainPage - Code Behind
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Browser;
     
    namespace DMCpro.Silverlight.ServiceCostingSheet
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                // set global telerik theme
                Telerik.Windows.Controls.StyleManager.ApplicationTheme = new Telerik.Windows.Controls.Office_SilverTheme();
     
                // Required to initialize variables
                InitializeComponent();
     
                // add event handles to keep the MainPage and HTML silverlightHostControl height in sync with its children
                this.LayoutRoot.SizeChanged += (s, e) => { this.Height = e.NewSize.Height; };
                this.SizeChanged += (s, e) => { HtmlPage.Document.GetElementById("silverlightControlHost").SetStyleAttribute("height", this.Height.ToString() + "px"); };
            }
        }
    }



    MainPage.XAML
    <UserControl x:Class="DMCpro.Silverlight.ServiceCostingSheet.MainPage"
        mc:Ignorable="d"
        xmlns:view="clr-namespace:DMCpro.Silverlight.ServiceCostingSheet.View">
     
        <Grid x:Name="LayoutRoot">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
     
            <view:ServiceCostingSheetView Height="Auto" />
        </Grid>
    </UserControl>
  9. Jerry T.
    Jerry T. avatar
    118 posts
    Member since:
    Jul 2012

    Posted 05 Jul 2011 Link to this post

    Ah...and using this for the MvvmHelpers:
    // ****************************************************************************
    // <copyright file="ViewModelBase.cs" company="GalaSoft Laurent Bugnion">
    // Copyright © GalaSoft Laurent Bugnion 2009
    // </copyright>
    // ****************************************************************************
    // <author>Laurent Bugnion</author>
    // <email>laurent@galasoft.ch</email>
    // <date>22.4.2009</date>
    // <project>GalaSoft.MvvmLight</project>
    // <license>
    // See license.txt in this project or http://www.galasoft.ch/license_MIT.txt
    // </license>
    // <LastBaseLevel>BL0008</LastBaseLevel>
    // ****************************************************************************
     
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Diagnostics.CodeAnalysis;
     
    namespace DMCpro.Silverlight.MvvmHelpers
    {
        /// <summary>
        /// A base class for the ViewModel classes in the MVVM pattern.
        /// <para>The IDisposable implementation of this class is obsolete, and
        /// should not be used anymore. It will be removed in a future version.
        /// </para>
        /// </summary>
        public abstract class ViewModelBase : INotifyPropertyChanged
        {
            /// <summary>
            /// Occurs when a property value changes.
            /// </summary>
            public event PropertyChangedEventHandler PropertyChanged;
     
            /// <summary>
            /// Raises the PropertyChanged event if needed.
            /// </summary>
            /// <param name="propertyName">The name of the property that
            /// changed.</param>
            [SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate",
                Justification = "This cannot be an event")]
            protected virtual void RaisePropertyChanged(string propertyName)
            {
                VerifyPropertyName(propertyName);
     
                var handler = PropertyChanged;
     
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(propertyName));
                }
            }
     
            [SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate",
                Justification = "This cannot be an event")]
            protected void RaisePropertyChanged(PropertyChangedEventArgs args)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, args);
                }
            }
     
            /// <summary>
            /// Verifies that a property name exists in this ViewModel. This method
            /// can be called before the property is used, for instance before
            /// calling RaisePropertyChanged. It avoids errors when a property name
            /// is changed but some places are missed.
            /// <para>This method is only active in DEBUG mode.</para>
            /// </summary>
            /// <param name="propertyName"></param>
            [Conditional("DEBUG")]
            [DebuggerStepThrough]
            public void VerifyPropertyName(string propertyName)
            {
                var myType = this.GetType();
                if (myType.GetProperty(propertyName) == null)
                {
                    throw new ArgumentException("Property not found", propertyName);
                }
            }
        }
    }
Back to Top
DevCraft banner