Hi, I am subclassing an ItemsControl (let's call it EnhancedItemsControl), and I would like to expose ScrollViewerTemplate dependency property, which would allow the user to optionally specify his own template for used ScrollViewer. I am doing it like this:
public ControlTemplate ScrollViewerTemplate { get { return (ControlTemplate)GetValue(ScrollViewerTemplateProperty); } set { SetValue(ScrollViewerTemplateProperty, value); } }
public static readonly DependencyProperty ScrollViewerTemplateProperty = DependencyProperty.Register( "ScrollViewerTemplate", typeof(ControlTemplate), typeof(EnhancedItemsControl), new UIPropertyMetadata(new ScrollViewer().GetValue(ScrollViewer.TemplateProperty))); //This doesn't work for me
In my default style for my EnhancedItemsControl, I then include the ScrollViewer like this:
This works when the user specifies the ScrollViewerTemplate, but when he leaves it at default value, the ScrollViewer is not displayed (presumably because it's Template is empty). How can I tell WPF Use the Template only when it is non-null, otherwise use default one? (it occured to me that I could use triggers to set the Template only when it is not null, but I don't like the idea of having a trigger for every custom property in each of my controls...)
There is similar problem with styles - if I wanted to let user specify the ScrollViewer style, but the user didn't specify it, the value of ScrollViewerStyle would be null (equal to <ScrollViewer Style="{x:Null}" />
), which would stop the default style from being applied!
How to solve this? Thank you!