private void SetTemplateBindingForCurrentFilterItem()
{
if (_currentFilterItem == null)
{
return;
}
// Assume we are not going to set up a binding for template. The user may using FilterItemContainerStyle with a Setter
// for HeaderTemplate. In this case, we need to honor that setter so long as an item template for the filter has not
// been set through one of the other APIs: FilterItemTemplate, AllFilterItemTemplate, FilterItemTemplateSelector.
// Thus, only set this binding on HeaderTemplate when one of those template APIs is specified. When a template for the filter item
// is not specified, we unset this binding so that a Setter for HeaderTemplate in the Style is able to bleed through.
bool templateShouldBeBound = false;
if (FilterItemTemplateSelector is RibbonGalleryDefaultFilterItemTemplateSelector)
{
if (object.ReferenceEquals(CurrentFilter, AllFilterItem))
{
if (this.AllFilterItemTemplate != null)
{
templateShouldBeBound = true;
}
}
else
{
if (this.FilterItemTemplate != null)
{
templateShouldBeBound = true;
}
}
}
else
{
// Someone is setting FilterItemTemplateSelector. We should bind to the template that gets selected.
templateShouldBeBound = true;
}
if (templateShouldBeBound && !FilterMenuButtonTemplateIsBound)
{
Binding currentFilterTemplateBinding = new Binding("CurrentFilterTemplate") { Source = this };
_currentFilterItem.SetBinding(RibbonMenuItem.HeaderTemplateProperty, currentFilterTemplateBinding);
FilterMenuButtonTemplateIsBound = true;
}
else if (!templateShouldBeBound && FilterMenuButtonTemplateIsBound)
{
BindingOperations.ClearBinding(_currentFilterItem, RibbonMenuItem.HeaderTemplateProperty);
FilterMenuButtonTemplateIsBound = false;
}
}