Home Forums WPF controls Xceed DataGrid for WPF Apply Converter in DataGrid

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #24666 |

    I’m applying a converter to a decimal field so it looks more like money. (The converter adds a $ and makes sure there are 2 decimals.)

    The only way I’ve found to apply the converter is through DisplayMemberBinding. This works for formatting the number, but also disables the column containing the data. How do I convert while still being able to edit? (See the Amount column below).

    <xcdg:DataGridControl x:Name=”AdjustmentGrid”
    CellEditorDisplayConditions=”MouseOverCell”
    EditTriggers=”BeginEditCommand,ActivationGesture”
    ItemScrollingBehavior=”Immediate” AutoCreateColumns=”False”
    ItemsSource=”{Binding Path=Adjustments, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type Window}}}”
    Margin=”0,3,0,3″
    Width=”Auto”
    FontSize=”9″
    HorizontalAlignment=”Center”
    MaxHeight=”120″ Height=”Auto”
    >
    <xcdg:DataGridControl.View>
    <xcdg:TableView ShowRowSelectorPane=”False” ShowFixedColumnSplitter=”False”>
    <xcdg:TableView.FixedHeaders>
    <xcdg:ClearHeadersFooters/>
    <DataTemplate>
    <xcdg:ColumnManagerRow/>
    </DataTemplate>
    </xcdg:TableView.FixedHeaders>
    </xcdg:TableView>
    </xcdg:DataGridControl.View>
    <xcdg:DataGridControl.Columns>
    <xcdg:Column FieldName=”Amount” Title=”Adjustment”
    DisplayMemberBinding=”{Binding Path=Amount,
    Converter={StaticResource numberformatter},
    ConverterParameter=C}”
    Width=”65″/>
    <xcdg:Column FieldName=”Descrip” Title=”Description”
    MinWidth=”175″
    MaxWidth=”175″/>
    </xcdg:DataGridControl.Columns>
    </xcdg:DataGridControl>

    Here is the number converter I’m using:

    namespace MPG.Library.Controls
    {
    public class NumberFormatter : IValueConverter
    {
    public object Convert(object value, Type targetType, object
    parameter, System.Globalization.CultureInfo culture)
    {
    try
    {
    return System.Convert.ToDecimal(value).ToString(parameter as string);
    }
    catch (Exception ex)
    {
    }
    return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter,
    System.Globalization.CultureInfo culture)
    {
    if (value.GetType().ToString() == “System.String”)
    {
    // Remove all characters except numbers, periods and a negative sign.
    string result = System.Text.RegularExpressions.Regex.Replace((string)value, “[^0-9.-]”, “”);
    return decimal.Parse(result);
    }
    else
    {
    return null;
    }
    }
    }
    }

    Imported from legacy forums. Posted by Brian (had 2133 views)

    User (Old forums)
    Member
    Post count: 23064

    Try

    DisplayMemberBinding=”{Binding Path=Amount,
    Converter={StaticResource numberformatter},
    ConverterParameter=C}, Mode=TwoWay”

    Imported from legacy forums. Posted by nat (had 549 views)

    Xceed Support
    Member
    Post count: 5658

    One drawback of the approach to set the Converter on the DisplayMemberBinding of the column is that the Editor will receive the “formatted” value as well (and your converter will need to convert back and forth the formatted value ).

    The approach I suggest is to instead place the converter in the CellContentTemplate so that the value of the column remains the unformatted value…

    <xcdg:Column>
    <xcdg:Column.CellContentTemplate>
    <DataTemplate>
    <TextBlock Text=”{Binding Path=., Converter={StaticResource numberformatter}, ConverterParameter=C}”/>
    </DataTemplate
    </xcdg:Column.CellContentTemplate>
    <xcdg:Column>

    Imported from legacy forums. Posted by Marcus [Xceed] (had 3626 views)

    User (Old forums)
    Member
    Post count: 23064

    Marcus,

    I tried to put the CellContentTemplate. It works fine, but it is having some issue while sorting.

    I have a DateTime column in which i am getting the Value in hh:mm format. Now i have to convert it to hh:mm AM/PM format. Its converting perfectly but the sorting is not proper. If i remove the converter and display in hh:mm format then the sorting is perfect.

    Where could be the problem ?

    Thanks,

    Saurabh 

    Imported from legacy forums. Posted by Saurabh (had 2271 views)

    User (Old forums)
    Member
    Post count: 23064
                            <xcdg:Column FieldName=”IsOpen” Title=”Opened” Width=”*” >
                                <xcdg:Column.CellContentTemplate>
                                    <DataTemplate>
                                        <TextBlock Text=”{Binding Converter={StaticResource BooleanConverter}}” />
                                    </DataTemplate>
                                </xcdg:Column.CellContentTemplate>
                            </xcdg:Column>
     
     
    BooleanConverter is converter that receives bool value and prints label. The value of converter is always null.

    Imported from legacy forums. Posted by tom123 (had 623 views)

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.