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

Dynamic DataFormatString binding using converter does not work

4 Answers 318 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Andy
Top achievements
Rank 1
Andy asked on 11 Apr 2012, 04:51 AM
Hi Telerik team,

I have a project with a requirement to display numeric value depending on dynamic unit format in radgridview or radtreelistview.

I bind my radtreelistview to list of object from class below =>
for example : ObservableCollection<MyVariable> obj = new ObservableCollection<MyVariable>();
radTreeListView1.ItemsSource = obj;

public class MyVariable : ViewModelBase, INotifyPropertyChanged
{
     public string Name { get; set; }
     public CellValue First { get; set; }
     public CellValue Second { get; set; }
     public CellValue Third { get; set; }
     public string Remarks { get; set; }
     public ObservableCollection<MyVariable> ChildCollection { get; set; }
 }
 
 public class CellValue : INotifyPropertyChanged
 {
     public string Key { get; set; }
     public double? Value { get; set; }
     public string Unit { get; set; }
     public string Location { get; set; }
     public bool IsEditable  { get; set; }
 }

 below is my xaml code for the grid.
<telerik:RadTreeListView x:Name="radTreeListView1" AutoGenerateColumns="False" RowIndicatorVisibility="Collapsed" RowLoaded="radTreeListView1_RowLoaded" RowIsExpandedChanging="radTreeListView1_RowIsExpandedChanging">
                                <telerik:RadTreeListView.ChildTableDefinitions>
                                    <telerik:TreeListViewTableDefinition ItemsSource="{Binding ChildCollection}" />
                                </telerik:RadTreeListView.ChildTableDefinitions>
                                <telerik:RadTreeListView.Columns>
                                    <telerik:GridViewDataColumn Header="Parameters" DataMemberBinding="{Binding Name}" IsReadOnly="True" Width="auto"  />
                                    <telerik:GridViewDataColumn Header="First Value" DataMemberBinding="{Binding First.Value, Mode=TwoWay}" DataFormatString="{Binding First.Unit, Converter={StaticResource numberConverter}}" IsReadOnlyBinding="{Binding First.IsEditable}" CellStyleSelector="{StaticResource cellStyle}" Width="auto" />
                                    <telerik:GridViewDataColumn Header="Second Value" DataMemberBinding="{Binding Second.Value, Mode=TwoWay}" DataFormatString="{Binding Second.Unit, Converter={StaticResource numberConverter}}" IsReadOnlyBinding="{Binding Second.IsEditable}" CellStyleSelector="{StaticResource cellStyle}" Width="auto"  />
                                    <telerik:GridViewDataColumn Header="Third Value" DataMemberBinding="{Binding Third.Value, Mode=TwoWay}" DataFormatString="{Binding Third.Unit, Converter={StaticResource numberConverter}}" IsReadOnlyBinding="{Binding Third.IsEditable}" CellStyleSelector="{StaticResource cellStyle}" Width="auto" />
                                    <telerik:GridViewDataColumn Header="Remarks" DataMemberBinding="{Binding Remarks}" IsReadOnly="True" Width="*" />
                                </telerik:RadTreeListView.Columns>
</telerik:RadTreeListView>

I also have a converter class to handle the DataFormatString
public class NumberConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        CellValue cellValue = value as CellValue;
        if (cellValue != null && cellValue.Unit == "%")
        {
            return "{0:p2}";
        }
        else
            return "{0:n2}";
    }
 
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value;
    }
}

When I run these codes. it never run through the code in numberConverter class. but if i change DataFormatString code in xaml code to 
<telerik:GridViewDataColumn Header="First Value" DataMemberBinding="{Binding First.Value, Mode=TwoWay}" DataFormatString="{Binding First, Converter={StaticResource numberConverter}}" IsReadOnlyBinding="{Binding First.IsEditable}" CellStyleSelector="{StaticResource cellStyle}" Width="auto" />
<telerik:GridViewDataColumn Header="Second Value" DataMemberBinding="{Binding Second.Value, Mode=TwoWay}" DataFormatString="{Binding Second, Converter={StaticResource numberConverter}}" IsReadOnlyBinding="{Binding Second.IsEditable}" CellStyleSelector="{StaticResource cellStyle}" Width="auto"  />
<telerik:GridViewDataColumn Header="Third Value" DataMemberBinding="{Binding Third.Value, Mode=TwoWay}" DataFormatString="{Binding Third, Converter={StaticResource numberConverter}}" IsReadOnlyBinding="{Binding Third.IsEditable}" CellStyleSelector="{StaticResource cellStyle}" Width="auto" />

It run through the numberConverter codes. if i put a break point in this line of code 
CellValue cellValue = value as CellValue; 

when i checked the type of "value" from above code. instead of "CellValue" it showed "[ProjectNamespace].MainWindow" as type of value.

I followed the code from this thread
http://www.telerik.com/community/forums/wpf/gridview/dataformatstring.aspx 

but it will bind the value as string and not double which causing the cell to be able to accept non numeric value.

Please suggest how to achieve this dynamic DataFormatString binding.
 
Thanks.
Andy

4 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 11 Apr 2012, 06:46 AM
Hi,

 Why not simply use this converter for the DataMemberBinding directly instead binding DataFormatString?

All the best,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Andy
Top achievements
Rank 1
answered on 11 Apr 2012, 07:54 AM
Hi Vlad,

Thank you for your reply.
If I use the converter directly to DataMemberBinding, it would change the actual value of the variable bound to the cell.
I need to keep the original value for the cell.

for example:
I use code in this thread as reference http://www.telerik.com/community/forums/wpf/gridview/dataformatstring.aspx  
the actual value is 55.3674 (double). the converter will convert the value with {0:N2} format

1st scenario :
1. I use the converter directly to DataMemberBinding which resulting the actual value become 55.37 (string).
2. User now can freely type non-numeric value into the cell. I know i can handle it using cellEditing handler. but it is automatically handled if the datatype is double.

2nd scenario:
1. I use the converter directly to DataMemberBinding which resulting the actual value become 55.37. convert it back to double.
2. When user edit the cell. they need to see the original value which is 55.3674. but the value has been modified upon conversion. 

my understanding is DataFormatString won't change the actual value because it acts like masking function. that is why I need to use DataFormatString. CMIIW.
Is there any other way to achieve this purpose?

Thanks,
Andy

0
Accepted
Vlad
Telerik team
answered on 11 Apr 2012, 08:00 AM
Hello,

 In this case you need to define CellTemplate with plain TextBlock where you can set the converter for the Text property Binding. The grid will use DataMemberBinding without converter for editing.

All the best,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Andy
Top achievements
Rank 1
answered on 11 Apr 2012, 08:59 AM
Thanks. It is working now :)
Tags
GridView
Asked by
Andy
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Andy
Top achievements
Rank 1
Share this question
or