SummaryTemplate

Grids for WPF Forum

Posted 12 years ago by Tom P.
Version: 11.1.0545
Avatar
I need to be able to have the Summary area of the PropertyGrid display the value of a Bitmap Property (as an image), when that property is selected. When the selected property is not a bitmap, it should revert back to the "default" SummaryTemplate.

I'm having a hard time figuring out how to accomplish this. I can see from the default ActiproXamlStyles.zip, that there is a style for PropertyGrid that sets the SummaryTemplate on load. I can also see that if I assign SummaryTemplate in my XAML, it will override this. That is all great. However, I presume I'll need to implement a SummaryTemplateSelector.

But, how can I have my SummaryTemplateSelector return the "default" template from the "default" ActiproXamlStyles? I'd rather not have to duplicate the XAML, I'd like to be able to look it up dynamically.

Comments (2)

Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Tom,

You are correct in that you'd need to implement a custom DataTemplateSelector and assign it to our PropertyGrid.SummaryTemplateSelector property. You'd also need to set SummaryTemplate to null (or in XAML use "{x:Null}"). Finally, you'd have to copy our default DataTemplate in to your resources so you can use it in the default case. There's no way to use SummaryTemplate and SummaryTemplateSelector simultaneously.


Actipro Software Support

Posted 12 years ago by Tom P.
Avatar
I really didn't want to copy/paste the XAML, so instead, I looked it up in the Application Resources. Works great! Below is my solution

Resources

  <DataTemplate
    x:Key="ImageSummaryTemplate">
    <Image
      Source="{Binding Value, Mode=OneWay}" />
  </DataTemplate>
  
    <res:SummaryTemplateSelector
      x:Key="summaryTemplateSelector"
      ImageTemplate="{StaticResource ImageSummaryTemplate}"/>

PropertyGrid XAML (abbreviated)

      <propgrid:PropertyGrid
        . . .
        SummaryTemplate="{x:Null}"
        SummaryTemplateSelector="{StaticResource summaryTemplateSelector}"
        . . .>

SummaryTemplateSelector.cs


namespace MyNamespace
{
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media.Imaging;
    using ActiproSoftware.Windows.Controls.PropertyGrid;
    using ActiproSoftware.Windows.Controls.PropertyGrid.Primitives;

    /// <summary>
    /// DataTemplate for the PropertyGrid Summary section.
    /// </summary>
    public class SummaryTemplateSelector : DataTemplateSelector
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the ImageTemplate property.
        /// </summary>
        public DataTemplate ImageTemplate { get; set; }

        /// <summary>
        /// Gets or sets the DefaultTemplate property.
        /// </summary>
        public DataTemplate DefaultTemplate { get; set; }

        #endregion

        #region Public Overrides

        /// <summary>
        /// When overridden in a derived class, returns a System.Windows.DataTemplate based on custom logic.
        /// </summary>
        /// <param name="item">The data object for which to select the template.</param>
        /// <param name="container">The data-bound object.</param>
        /// <returns>Returns a System.Windows.DataTemplate or null. The default value is null.</returns>
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            if (this.DefaultTemplate == null)
            {
                // Grab the default Style if it exists.
                Style style = Application.Current.TryFindResource(typeof(PropertyGrid)) as Style;
                if (style != null)
                {
                    // Grab the default SummaryTemplate if it exists.
                    Setter setter = style.Setters.OfType<Setter>().FirstOrDefault(s => s.Property == PropertyGrid.SummaryTemplateProperty);
                    if (setter != null)
                    {
                        this.DefaultTemplate = setter.Value as DataTemplate;
                    }
                }
            }

            DataTemplate template = this.DefaultTemplate;

            PropertyDescriptorDataAccessor pdda = item as PropertyDescriptorDataAccessor;
            if (pdda != null)
            {
                if (pdda.PropertyDescriptor.PropertyType == typeof(BitmapImage))
                {
                    template = this.ImageTemplate;
                }
            }

            return template;
        }

        #endregion
    }
}
The latest build of this product (v24.1.2) was released 2 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.