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

[Solved] Reusable Editable Currency Column

3 Answers 122 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Ross Wozniak
Top achievements
Rank 1
Ross Wozniak asked on 12 Nov 2009, 02:21 PM
I am currently using code like this repeatedly in my RadGridViews:

 

 

<telerikGridView:GridViewDataColumn Header="Total Paid" DataMemberBinding="{Binding MyAmount, Mode=TwoWay}" DataFormatString="{}{0:c}" TextAlignment="Right" Width="100">

 

 

 

<telerikGridView:GridViewDataColumn.CellTemplate>

 

 

 

<DataTemplate>

 

 

 

<TextBlock Text="{Binding MyAmount, Converter={StaticResource CurrencyConverter}}" TextAlignment="Right"/>

 

 

 

</DataTemplate>

 

 

 

</telerikGridView:GridViewDataColumn.CellTemplate>

 

 

 

<telerikGridView:GridViewDataColumn.CellEditTemplate>

 

 

 

<DataTemplate>

 

 

 

<telerikInput:RadMaskedTextBox MaskType="Numeric" Mask="c" Value="{Binding MyAmount, Mode=TwoWay}" TextAlignment="Right" Loaded="Editor_Loaded"/>

 

 

 

</DataTemplate>

 

 

 

</telerikGridView:GridViewDataColumn.CellEditTemplate>

 

 

 

</telerikGridView:GridViewDataColumn>

I would like to create a reusable class that extends GridViewDataColumn that has all of this built in, so I don't have to repeat the code over and over. Do you have any samples of how to achieve this?

Thanks,
Ross

 

3 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 12 Nov 2009, 02:26 PM
Hello Ross,

Please check this post for more info:
http://blogs.telerik.com/nedyalkonikolov/posts/09-11-12/how_to_create_custom_editor_with_radgridview_for_silverlight_and_wpf.aspx

Greetings,
Vlad
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Ross Wozniak
Top achievements
Rank 1
answered on 12 Nov 2009, 02:56 PM
Thanks very much for the suggestion Vlad. I implemented the class as such, and it seems to be working properly. Cleaned up my code a lot! Of course, I am using the CurrencyFormatter from the Silverlight.FX library...but any CurrencyConverter will do.

 

 

using

 

System.Windows;

 

using

 

System.Windows.Controls;

 

using

 

System.Windows.Data;

 

using

 

SilverlightFX.Data;

 

using

 

Telerik.Windows.Controls;

 

using

 

Telerik.Windows.Controls.GridView;

 

namespace

 

PurepaySpreadsheets.CustomControls

 

{

 

public class GridViewCurrencyColumn : GridViewBoundColumnBase

 

{

 

public GridViewCurrencyColumn()

 

{

DataFormatString =

"{0:c}";

 

}

 

public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)

 

{

 

var cellElement = new TextBlock {TextAlignment = TextAlignment.Right};

 

 

var valueBinding = new Binding(DataMemberBinding.Path.Path)

 

{

Mode =

BindingMode.OneTime,

 

Converter =

new CurrencyFormatter()

 

};

cellElement.SetBinding(

TextBlock.TextProperty, valueBinding);

 

 

return cellElement;

 

}

 

public override FrameworkElement CreateCellEditElement(GridViewCell cell, object dataItem)

 

{

 

var cellEditElement = new RadMaskedTextBox {MaskType = MaskType.Numeric, Mask = "c", TextAlignment = TextAlignment.Right};

 

BindingTarget =

RadMaskedTextBox.ValueProperty;

 

 

var valueBinding = CreateValueBinding();

 

cellEditElement.SetBinding(

RadMaskedTextBox.ValueProperty, valueBinding);

 

 

return cellEditElement;

 

}

 

private Binding CreateValueBinding()

 

{

 

return new Binding

 

{

Mode =

BindingMode.TwoWay,

 

NotifyOnValidationError =

true,

 

ValidatesOnExceptions =

true,

 

UpdateSourceTrigger =

UpdateSourceTrigger.Explicit,

 

Path =

new PropertyPath(DataMemberBinding.Path.Path)

 

};

}

}

}

0
Ross Wozniak
Top achievements
Rank 1
answered on 12 Nov 2009, 03:00 PM
Sorry for the ugly code formatting. I guess I'm not sure what the best way to post code in here is. I tried the FormatCodeBlock option, but it inserted a ton of HTML tags.
Tags
GridView
Asked by
Ross Wozniak
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Ross Wozniak
Top achievements
Rank 1
Share this question
or