Styled Editable Generic Currency GridViewDataColumn

1 Answer 22 Views
GridView
Heath
Top achievements
Rank 1
Heath asked on 27 Feb 2025, 11:04 PM
Hi all

I have tried all sorts of ways (BindingProxy, Converters, DP Helpers..) but I can't seem to get it to work.

I have this:

  <telerik:GridViewDataColumn
      Header="Unit Cost"
      DataMemberBinding="{Binding UnitCost}"
      Style="{StaticResource Style.GridViewDataColumn.Currency}"
      Width="120" />

And the style (simplified) is this:

<Style
    x:Key="Style.GridViewDataColumn.Currency"
    TargetType="{x:Type telerik:GridViewDataColumn}">
    <Setter
        Property="DataFormatString"
        Value="N2" />

    <!-- Cell Template (Display Mode) -->
    <Setter
        Property="CellTemplate">
        <Setter.Value>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition
                            Width="Auto" />
                        <ColumnDefinition
                            Width="*" />
                    </Grid.ColumnDefinitions>
                    <TextBlock
                        Text="$"
                        VerticalAlignment="Center"
                        Margin="5,0" />
                    <TextBlock
                        Text="{Binding ??, StringFormat=N2}"
                        VerticalAlignment="Center"
                        HorizontalAlignment="Right"
                        Grid.Column="1" />
                </Grid>
            </DataTemplate>
        </Setter.Value>
    </Setter>

    <!-- Cell Edit Template -->
    <Setter
        Property="CellEditTemplate">
        <Setter.Value>
            <DataTemplate>
                <telerik:RadMaskedCurrencyInput
                    Value="{Binding Path=??, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                    IsTabStop="True" />
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

How can I define the Path generically. If I hard code UnitCost it works, but I need it to be generic, and I can't find a way to do it.

PS: Design breif is that our company wants to display the $ sign on the left and the value aligned right.

Any insight would be welcomed greatly.

1 Answer, 1 is accepted

Sort by
0
Stenly
Telerik team
answered on 04 Mar 2025, 09:44 AM

Hello Heath,

To achieve this behavior, you can create a UserControl, which will create a Binding instance for the Text property of the TextBlock element (non-edit mode) and the same approach can be followed when the cell goes into edit mode (set the binding for the Value property of the RadMaskedCurrencyInput). For the path of the Binding, you can retrieve the GridViewDataControl from the DataColumn property of the parent GridViewCell that can be retrieved using the ParentOfType extension method. Then, you can use these UserControls in the DataTemplate for the Setters of the CellTemplate and CellEditTemplate properties of the global Style for the GridViewDataColumn type.

With this being said, I attached a sample project that showcases this approach.

Regards,
Stenly
Progress Telerik

Enjoyed our products? Share your experience on G2 and receive a $25 Amazon gift card for a limited time!

Tags
GridView
Asked by
Heath
Top achievements
Rank 1
Answers by
Stenly
Telerik team
Share this question
or