locked
Databinding to a ConverterParameter - on a DataGrid RRS feed

  • Question

  • I have a scenario where I am presenting different data blocks in one DataGrid.

    I have the need that some cells of the same column would have to be presented without decimal numbers, others with decimals and others in Percentage representation.

    There is any way to do this?

    The most feasible solution I have thought is to bind a custom Converter through a value on the data that tells it the format that must be applied, then to bind it through the ConverterParameter. I've tried to bind directly the StringFormat & the ConverterParameter but these don't work directly.

    I've searched the forums but found no working solution...

    Any help would be more than welcome.

    For a business application it is not uncommon to have different information groups and depending on the information, to represent itself in different formats... so it should be something easy or at least not "almost impossible" as it is now.

    I have thought and-or read abou some possible solutions..

    1. Use personalized converters that using the converter parameter provide different formats (the title of the post). then to databind the converterParameter to the data, so it can change its representation on each row.

    2. Using LoadingRow to change dynamically the cell binding - only issue is that this is a Column binding... or anybody knows the "internals"?

    3. Implement a custom Converter that has a DependencyProperty that IS bindable (it can be done if the converter is also a UserControl, as explained by Brandon Tuong on his blog (check here: http://brandontruong.wordpress.com/2009/06/09/binding-for-converter/ this is a very smart solution, but I haven't gotten past the point of implementing the custom converter with the DependencyProperty and using it as a normal one..)

     

    Thanks,

    Jose

    Friday, August 27, 2010 5:28 AM

Answers

All replies

  • Another possiblilty is to add a property to your class you can bind to which does the formatting for you.  That way you have access to all the data in the class so you can decide which way to format the data.

    Friday, August 27, 2010 6:21 AM
  • Hi Ken,

    First, thanks for the quick reply.

    I have done so on the data collection. Let's say I have a collection of different kind of projects, there  I have the Expenses value and then the Expenses_format, then I bind a ObservableCollection<Project> to the datagrid.

    Also I want to use the Binding system & the ConverterParameter for this using this field/property on my class, just as you say, I already know what do I want to format my data to. It's just the how do I do it.

    Let's say I've got a class with Four properties: Type, Title, Expense_value, Expense_StringFormat.

    Then a Collection of these classes which I bind a DataGrid to.

    I want to use the Binding System because some of this data will be editable too.

    The result should be something like this:

    Type Expense / Pct Completion
    Project type A
    Resource X 100,00 €
    Resource X 20,00 €
    Resource X 50,00 €
    Project type B
    Resource X 25.000 €
    Resource X 35.000 €
    Resource X 55.300 €
    Project type C
    Resource X 50%
    Resource X 20%
    Resource X 60%

    For adding a bit of detail to the implementation, the actual converter is this one:

    public class GenericConverter : FrameworkElement, IValueConverter
    {

    dpConverterParameterBinding =
        public static readonly DependencyProperty dpConverterParameterBindingProperty =
                                DependencyProperty.Register("MyProperty", typeof( string ), typeof(GenericConverter), new PropertyMetadata(dpConverterParameterBindingProperty_Changed));    
         public string ConverterParameterBinding
        {
                set {
                    this.SetValue(dpConverterParameterBindingProperty, value);
                }
                get {
                    return (string)this.GetValue(dpConverterParameterBindingProperty);
                }
            }

        private static void dpConverterParameterBindingProperty_Changed(object sender, DependencyPropertyChangedEventArgs args)   
        {    
        
        }

        public String GetConverterParameter(String parameter) {
            if (!string.IsNullOrEmpty(ConverterParameterBinding)) {
                return ConverterParameterBinding;
            }
               
            if (!string.IsNullOrEmpty(parameter))
            {
                return parameter;
            }

            return String.Empty;
        }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return string.Empty;

            String ConverterParamToApply = GetConverterParameter(parameter as String);

            if (!string.IsNullOrEmpty(ConverterParamToApply))
            {
                return string.Format(culture, ConverterParamToApply, value);
            }
            else {
                return value.ToString();
            }
        }        

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
           
    }


    And what I would love is get to work something like this :

     <data:DataGridTextColumn Header="Expense"
    Binding="{Binding Converter={StaticResource GenericConverter}, ConverterParameter={Binding Expense_FormatString}, Mode=TwoWay, Path=Expense_Value}" >

    So basically the issue is not to know how should I format the data or having this data ready, but how to format it conditionally without affecting the value of the data field (Expense_Value).

    If you think there is a way (complicated or not) to do this, I am eager to see it & learn :).

     

    Thanks!!

     

     

     

     

     

     

    Friday, August 27, 2010 6:58 AM
  • I am now facing a similar issue.

    Did you find any solution?

    Thursday, September 16, 2010 7:54 AM
  • Hi Michelle,

     

    Yeah! at last :)

    Sorry I did not post it here, I thought I did but... well...

    You can read about the solution here: http://silverlightguy.com/2010/09/02/silverlight-datagrid-solutions/

    Please let me know if you have any doubt...

    It's been a horrible limitation and wasted a lot of time in "solving" it...

     

    Hope it helps :)

    Thursday, September 16, 2010 10:54 AM