Default format strings

8 posts, 0 answers
  1. hamish
    hamish avatar
    44 posts
    Member since:
    Jun 2007

    Posted 17 Sep 2014 Link to this post

    I am trying to set default format strings for the individual elements in the class I using as the item data source. . For the display name decorating the class element as follows, works just fine and the word Account appears in the field list as I would expect. However when I try and do the same thing for a default format as shown in the 2nd code snippet it does nothing. How do I set default format strings - thanks​
    [Display(Name = "Account")]
     public string accnt { get; set;} 
    [DisplayFormat(DataFormatString="#,##0.0000")]
     public double cnOpened { get; set; }
  2. Kalin
    Admin
    Kalin avatar
    1209 posts

    Posted 19 Sep 2014 Link to this post

    Hello Hamish,

    In order to achieve the desired you would need to set the desired StringFormat in the Xaml definition of the needed AggregateDescription as demonstrated below:

    <pivot:PropertyAggregateDescription PropertyName="Quantity" StringFormat="#.#0" />

    This will initially show the AggregateDescription with that format, however when modifing the Pivot at run time this format will be lost. So you would also need to hook to the PrepareDescriptionForField event of the provider in order to apply the format every time the fields in the FieldList are changed as shown below:

    private void LocalDataSourceProvider_PrepareDescriptionForField(object sender, PrepareDescriptionForFieldEventArgs e)
    {
        var aggregateDescription = e.Description as PropertyAggregateDescriptionBase;
      
        if (e.DescriptionType == Telerik.Pivot.Core.DataProviderDescriptionType.Aggregate && aggregateDescription != null && aggregateDescription.PropertyName == "Quantity")
        {
            aggregateDescription.StringFormat = "#.#0";
        }
    }

    Hope this helps.

    Regards,
    Kalin
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  3. UI for WPF is Visual Studio 2017 Ready
  4. Brett
    Brett avatar
    10 posts
    Member since:
    Dec 2013

    Posted 08 Dec 2015 in reply to Kalin Link to this post

    Hi Kalin -

    I would also like to setup default format strings for my aggregated values in my application. I am following the MVVM pattern, and am instantiating my LocalDataSourceProvider within the code of my view model. As a result, I cannot hook into the PrepareDescriptionForField event in my view's code-behind, but I understand I can use EventToCommandBehavior (http://docs.telerik.com/devtools/wpf/common-event-to-command-behavior.html) to setup a binding between the PrepareDescriptionForField event and a custom command in my view model to achieve this.

    However, I am having trouble getting this to work. It seems the command is not firing when I make changes in my RadPivotFieldList. Can you offer an guidance or an example of wiring this up correctly?

    My LocalDataSourceProvider is called CommissionDataProvider, and the relevant code snippet is shown here:

    TestCommand = new RelayCommand(Test);
    EventBinding eventBinding = new EventBinding();
    eventBinding.Command = TestCommand;
    eventBinding.EventName = "PrepareDescriptionForField";
    eventBinding.RaiseOnHandledEvents = true;
    eventBinding.PassEventArgsToCommand = true;
    EventToCommandBehavior.GetEventBindings(CommissionDataProvider).Add(eventBinding);

    Thanks,
    Brett

  5. Brett
    Brett avatar
    10 posts
    Member since:
    Dec 2013

    Posted 09 Dec 2015 Link to this post

    As a follow-up,

    I have also changed my DataProvider binding to TwoWay, but that did not help either.

    <pivot:RadPivotGrid Grid.Row="0" Grid.Column="0"
                        DataProvider="{Binding Path=CommissionDataProvider, Mode=TwoWay}"
                        telerikControls:StyleManager.Theme="Windows7"
                        HorizontalLayout="Tabular"
                        VerticalLayout="Compact"
                        RowSubTotalsPosition="Bottom"
                        RowGrandTotalsPosition="Bottom">
        <pivot:RadPivotGrid.RowGroupsExpandBehavior>
            <pivot:GroupsExpandBehavior Expanded="False" />
        </pivot:RadPivotGrid.RowGroupsExpandBehavior>
    </pivot:RadPivotGrid>
     
    <pivot:RadPivotFieldList Grid.Row="0" Grid.Column="1"
                             DataProvider="{Binding Path=CommissionDataProvider, Mode=TwoWay}"
                             telerikControls:StyleManager.Theme="Windows7" />

    Any guidance?

    Thanks,
    Brett

  6. Kalin
    Admin
    Kalin avatar
    1209 posts

    Posted 11 Dec 2015 Link to this post

    Hi Brett,

    The reason for the EventToCommandBehavior not working in this scenario is that the behavior is designed to work only with UIElements and the LocalDataSourceProvider inherits from DependencyObject. So I'm afraid you won't be able to use it in this particular scenario.

    If you have any other questions, please let us know.

    Regards,
    Kalin
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  7. Brett
    Brett avatar
    10 posts
    Member since:
    Dec 2013

    Posted 11 Dec 2015 in reply to Kalin Link to this post

    Hi Kalin - 

    Thank you for the reply. In this case, since I cannot use the EventToCommandBehavior functionality, what are my options for setting default format strings for the aggregate values, while still keeping the LocalDataSourceProvider in my view model? 

    Thanks,
    Brett

  8. Kalin
    Admin
    Kalin avatar
    1209 posts

    Posted 15 Dec 2015 Link to this post

    Hi Brett,

    Since you have the LocalDataSourceProvider defined in the ViewModel, wouldn't be suitable for you to hook to the event directly there? If you don't want to use events in the ViewModel you could implement an attached behavior for the LocalDataSourceProvider and do the hooking there.

    Hope this helps.

    Regards,
    Kalin
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  9. Brett
    Brett avatar
    10 posts
    Member since:
    Dec 2013

    Posted 15 Dec 2015 in reply to Kalin Link to this post

    Hi Kalin -

    Thank you! As you suggested, I was able to simply add the event handler in the code of my view model.

    CommissionDataProvider = new LocalDataSourceProvider();
    CommissionDataProvider.PrepareDescriptionForField += CommissionDataProvider_PrepareDescriptionForField;

    I appreciate the help.

    Best,
    Brett

Back to Top
UI for WPF is Visual Studio 2017 Ready