Home Forums WPF controls Xceed DataGrid for WPF Dynamically changing the FixedFooters DataTemplate

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #21705 |

    I am adding a footer to the datagrid that shows a summary of the selected row, similar to a master-detail view. However, I want to display a different DataTemplate depending on the type of records being displayed.

    There is a combo box that is used to control the contents of the collection being bound to the datagrid, so this collection will contain different types of record depending on the value selected.

    I have defined a couple of DataTemplate blocks and given them different x:Key values and created a custom DataTemplateSelector class that will return the appropriate DataTemplate. Is it possible to tell the FixedFooters element which DataTemplate to use in the XAML or do I need to do this programmatically?

    Imported from legacy forums. Posted by Scott (had 5675 views)

    Xceed Support
    Member
    Post count: 5658

    You could create a single DataTemplate, placed in your FixedFooters, that would automatically detect the type of the SelectedItem and take appropriate measure through the use of triggers.

    Here is a small example:

    <code>
    <DataTemplate x:Key=”myFixedFooter”>
    <ContentPresenter x:Name=”contentPresenter” Content=”{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridControl}}, Path=SelectedItem}” ContentTemplateSelector=”{StaticResource myTemplateSelector}”/>
    </DataTemplate>
    </code>

    Now, all your Selector has to do is do a lookup of the type of the value passed…

    Additionally (in bonus), the DataContext of your DataTemplate will be the data object you wish to show information about.

    Imported from legacy forums. Posted by Marcus [Xceed] (had 542 views)

    Xceed Support
    Member
    Post count: 5658

    Correction:

    In the first paragraph of my previous post, instead of:

    “that would automatically detect the type of the SelectedItem and take appropriate measure through the use of triggers.”

    I meant: “that would bind to the DataGridControl’s SelectedItem and select the appropriate template based on the selected item’s type.

    Imported from legacy forums. Posted by Marcus [Xceed] (had 562 views)

    User (Old forums)
    Member
    Post count: 23064

    That’s exactly what I was looking for. It required a trivial change to the selector to deal with the actual data type rather than the string value coming from the combo box, but it works perfectly.

    Imported from legacy forums. Posted by Scott (had 437 views)

    User (Old forums)
    Member
    Post count: 23064

    I have a followup question to this…

    If the collection I bind to does not contain any rows, I still display the column headers and I still want to display a footer based on the type of data that would be contained in the collection. I thought that I would be able to get access to the actual data context (or items source) through the DependencyObject that is passed to my DataTemplateSelector, but this ends up being an object of type ContentPresenter. I don’t see a way to walk back up the tree in the C# code to find the containing datagrid.

    Imported from legacy forums. Posted by Scott (had 612 views)

    Xceed Support
    Member
    Post count: 5658

    You can try something like this… ( for the FixedFooter template)

    <code>
    <DataTemplate>
    <ContentPresenter Content=”{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridControl}}, Path=ItemsSource, Converter={StaticResource myCustomConverter}}”
    ContentTemplateSelector=”{StaticResource mySelector}”/>
    </DataTemplate>
    </code>

    The job of the Converter specified on the Binding affected on the Content property would be to convert whatever information you can fetch on the ItemsSource into a token that can be identified by the ContentTemplateSelector…

    In your converter, you could:

    – check the type of the collection (if a typed list: e.g. List<ObjectA>, List<ObjectB>, …)
    – check for the presence of an attached property on the object (if a DependencyObject)

    You are not event forced to bind to the ItemsSource, you could bind to something else, which could be “tagged” with the information you need…

    Imported from legacy forums. Posted by Marcus [Xceed] (had 583 views)

    User (Old forums)
    Member
    Post count: 23064

    Thanks again. Using a custom converter worked perfectly.

    Imported from legacy forums. Posted by Scott (had 6747 views)

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.