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

Recreating template for GridView column

3 Answers 48 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Goran
Top achievements
Rank 1
Goran asked on 31 Jul 2012, 09:40 PM
Hi,

I have created new GridViewDateTimeColumn that will handle DateTime properties. In this column I have added one dependency property that will define StringFormat for DateTime values.

// Dependency Property
public static readonly DependencyProperty StringFormatProperty =
             DependencyProperty.Register("StringFormat", typeof(string),
             typeof(GridViewDateTimeColumn), new FrameworkPropertyMetadata(string.Empty, StringFormatChanged));
 
public string StringFormat
{
    get { return (string)GetValue(StringFormatProperty); }
    set { SetValue(StringFormatProperty, value); }
}
 
private static void StringFormatChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var column = (GridViewDateTimeColumn)d;
    column.DataMemberBinding.StringFormat = e.NewValue.ToString();
    column.Refresh();
}

xaml

<my_controls:GridViewDateTimeColumn DataMemberBinding="{Binding SomeDateTimeProperty, Mode=TwoWay}"
                 StringFormat="{Binding DataContext.StringFormat, RelativeSource={RelativeSource AncestorType=UserControl}}"
                                                        >

However, since the cell/column layout is created only once, I am unable to force the Column to rebind its values. StringFormatChanged is executed, and StringFormat property for column DataMemberBinding is set correctly, but original StringFormat is still active.

How can I make this work?

Regards,
Goran

3 Answers, 1 is accepted

Sort by
0
Goran
Top achievements
Rank 1
answered on 02 Aug 2012, 02:08 PM
Anyone, please? It is kind of urgent matter.

If I am not being clear, GridView control is bound to ObservableList<SomeType>. SomeType has a Duration property of type TimeSpan. Requirement:

1) Whenever I load list items in GridView, Duration should be displayed based on the StringFormat that I need to pass (exposed in ViewModel). It can be HH:mm, HH:mm:ss, dd:HH:mm, etc.

2) During View lifetime, GridView will load list of items many times, by user request, and the StringFormat can be changed (ex from HH:mm:ss to dd:HH:mm).

3) So basically, I need dynamic StringFormat, and this is the reason why I decided to create a StringFormat DependencyProperty.

4) The same request I need also for one DateTime property, where sometimes I need to display Date+time, sometimes only Date, and sometimes I need only Time

Regards, Goran
0
Ivan Ivanov
Telerik team
answered on 03 Aug 2012, 01:26 PM
Hi,

Have you tried rebinding the RadGridView instance, by invoking its Rebind() method? I guess that this should do the trick.

Greetings,
Ivan Ivanov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Goran
Top achievements
Rank 1
answered on 03 Aug 2012, 01:49 PM
Hi Ivan,

yes I have, I have tried all methods like Refresh, Rebind, from Column to GridView, and no success. I really dont understand why, I can confirm 100% that StringFormat for the binding was set. Maybe Rebind doesn't rebuild the binding for the cell, but only refreshes current binding. In the end, I decided to go with converter, and binding to entity, instead of property, so I can get required info from object hierarchy.

Regards,
Goran
Tags
GridView
Asked by
Goran
Top achievements
Rank 1
Answers by
Goran
Top achievements
Rank 1
Ivan Ivanov
Telerik team
Share this question
or