We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Binding base class property against multiple columns to use ValueConverter to change results

Hello,
I have a base class called Security with multiple subclasses (Stock,  Future, etc)
public class Security
{
    public string Symbol {get;set;}
}
 
public class Stock:Security
{
    public string RIC {get;set;}
    public string Sector {get;set;}
}
 
public class Future:Security
{
     public string RIC{get;set;}
     public DateTime Expiration {get;set;}
}
 
These securities are used as properties in an Order class
 
public class Order
{
    public string OrderID {get;set;}
    public Security Security{get;set;}
}
 
My viewmodel exposes a ObservableCollection<Order> where the Order's Security could be Future or Stock. I am adding columns to a GridDataControl which is bound to the collection of orders. I then use a ValueConverter in the binding to determine which subclass type each Security is so that I can pull the property that should be exposed for that type. In the case of the Expiration column that I added below, I return null for any Stock securities since they don't have that property, but return the proper value for the Futures.
 
public class SecurityToExpiryConverter:IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    Security sec = value as Security;
    if (sec == null) throw new ArgumentException("value", "Must be of type Security");
    Future f = sec as Future;
    if (f !=null) return f.Expiration.ToString("mm/DD/yyyy");
    return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    throw new NotImplementedException();
}
}
 
XAML below
 
  <Window.Resources>
        <c:SecurityToExpiryConverter x:Key="ExpiryConverter" />
        <c:SecurityToUnderlierConverter x:Key="UnderConverter" />
    </Window.Resources>
....
<syncfusion:GridDataVisibleColumn
HeaderText="Ticker"
MappingName="Security"
Binding="{Binding Path=Security, Converter={StaticResource UnderConverter}}"
AutoFit="True" IsReadOnly="True" AllowFilter="True"
/>
<syncfusion:GridDataVisibleColumn
HeaderText="Expiration"
MappingName="Security"
Binding="{Binding Path=Security,Converter={StaticResource ExpiryConverter}}"
AutoFit="True" IsReadOnly="True" AllowFilter="True"
/>
My problem is that even though I am specifying a specific binding for each of these VisibleColumns with their own ValueConverter, the only value converter that gets used is the one used in the first column for given MappingName. This makes the values displayed in these two columns the same, and in this case they will equal the Ticker column's ValueConverter output. If you switch the order of the columns in the XAML, they will both use the ExpiryConverter.
 
These columns both have the same MappingName="Security" right now, which I know is not right, but I am not sure what I should switch it to. This mapping name needs to be pointed to a property on the Order object, and it looks like if I point it at any property that is not bound yet (such as OrderID), it will work, even if that property is unrelated to the Path used in the actual Binding. That is a nasty hack, and not easily supported beyond this simple example.
 
I guess GridDataControl uses some internal Dictionary keyed off the MappingName's to the binding's, even if you specify your own Binding, and that is making the grid use the same ValueConverter.
Any suggestions on a way around this?
Thanks,
Chris

1 Reply

SH Shakul Hameed M Syncfusion Team November 22, 2013 08:51 AM UTC

Hi Chris,

Thanks for contacting Syncfusion support.

We have analyzed your query. Our GridDataControl displays the value based on reflection. Hence you can’t achieve this requirement by using GridDataControl. However you can achieve this requirement by using our SfDataGrid. We have created the sample based on your requirement and please check the sample under following location,

Sample: SfGridWithValueConverter.zip

Please let us know if you need further assistance.

Regards,

Shakul Hameed


Loader.
Live Chat Icon For mobile
Up arrow icon