This is a migrated thread and some comments may be shown as answers.

MVVM Caluculated Column with outside value

3 Answers 115 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Daniel
Top achievements
Rank 1
Daniel asked on 16 Dec 2015, 03:11 PM

Hi,

I have a list of workers, whom all needs to have an amount paid by way of their hours times a value.

I have a WorkerModel class:

public class WorkerModel
{
    public string Name { get; set; }
 
    public int HoursWorking { get; set; }
}

Then I have a CompanyModel that contains the workers:

public class CompanyModel
{
    private ObservableCollection<WorkerModel> _workers = new ObservableCollection<WorkerModel>();
 
    public ObservableCollection<WorkerModel> Workers
    {
        get { return _workers ; }
        set { _workers = value; }
    }
}

And then I have a ViewModel that allows me to bind to the view:

public class MainViewModel : ViewModelBase
{
    private readonly CompanyModel _company;
 
    public MainViewModel()
    {
        _company= new CompanyModel();
             
    }
 
    public ObservableCollection<CompanyModel > Workers
    {
        get { return _company.Workers; }
    }
 
    public decimal PayoutPerHour
    {
        get { return 71M; }
    }
 
 
}

Lastly my view looks like this:

 

<telerik:RadGridView ShowGroupPanel="False"
                             HorizontalAlignment="Left"
                             telerik:StyleManager.Theme="Summer"
                             AutoGenerateColumns="False"
                             GroupRenderMode="Flat"
                             NewRowPosition="Bottom"
                             ItemsSource="{Binding Workers}"
                             Grid.Column="0">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn Header="Name" DataMemberBinding="{Binding Path=Name}" Width="230"></telerik:GridViewDataColumn>
                <telerik:GridViewExpressionColumn Header="Payout" Width="100" Expression="HoursWorking * PayoutPerHour"></telerik:GridViewExpressionColumn></telerik:GridViewDataColumn>
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>

 

My goal is to have the HoursWorking from the WorkerModel, multiply with the PayoutPerHour thats in the ViewModel. I have in this example made the PayoutPerHour a static value, which it is not in my full example. I need the Expressioncolumn to update whenever either of those two properties are updated. But I can't tell the viewmodel what the WorkerModel is doing, since they are in a collection in the CompanyModel.

Any clever way to do this?

3 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 21 Dec 2015, 11:49 AM
Hello Daniel,

The most convenient approach for such scenario, would be the WorkerModel to implement INotifyPropertyChanged interface. By doing so, you can subscribe to the PropertyChanged event of each object in the ViewModel and listen for a value change. When such occurs, you can update the ExpressionColumn as per your requirements.

Best Regards,
Stefan X1
Telerik
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 Feedback Portal and vote to affect the priority of the items
0
shree
Top achievements
Rank 1
Veteran
answered on 13 Dec 2020, 04:59 PM
Extending on the previous question, can we bind the expression column results to DataMembers of the collection? Just like storing the calculated value into business objects.
I have 21 columns in my grid and one of the ExpressionColumn is based on already calculated expression in other column.
0
Martin Ivanov
Telerik team
answered on 16 Dec 2020, 02:58 PM

Hello Shrinath,

There is no built-in API that allows this. To achieve your requirement, you can use an expression directly into the view model and use a GridViewDataColumn instead of expression one. This way you can evaluate the expression once in the view model and then bind the result to as much columns as you need. I hope this helps.

Regards,
Martin Ivanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
GridView
Asked by
Daniel
Top achievements
Rank 1
Answers by
Stefan
Telerik team
shree
Top achievements
Rank 1
Veteran
Martin Ivanov
Telerik team
Share this question
or