Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6593489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:41:18+00:00 2026-05-25T17:41:18+00:00

I have a control in a base assembly with Content that I would like

  • 0

I have a control in a base assembly with Content that I would like to set based on the current DataContext.

To do so, I am trying to use a resource and subclassed DataTemplateSelector in the calling assembly as shown below. My initial hope was that the sub classed DataTemplateSelector would be called, but it isn’t. Then I tried adding an entry in the Resource Dictionary of the calling assembly with the same key but the sub classed selector, but that doesn’t get it done either.

Is there a way to fix the code I have to make this work? Is there a better strategy to set my content from the calling assembly?

Cheers,
Berryl

User Control (base assembly)

<UserControl 
    ...

    <Grid>
        <Border Style="{StaticResource FilterPanelBorderStyle}">
            <StackPanel Orientation="Horizontal" x:Name="myFilterPanel" >

      *****     <ContentControl x:Name="ctrlFilters" ContentTemplateSelector="{StaticResource filterControlsTemplateSelector}" /> ****

                <Button x:Name="btnClearFilter" Style="{StaticResource FilterPanelClearButtonStyle}" />
                <Label x:Name="lblStatus" Style="{StaticResource FilterPanelLabelStyle}" Content="{Binding Status}" />

            </StackPanel>
        </Border>

    </Grid>
</UserControl>

Resources and DataTemplateSelector (base assembly)

<views:FilterControlsTemplateSelector x:Key="filterControlsTemplateSelector"/>

<DataTemplate x:Key="defaultFilterContent">
    <TextBlock>Replace ME with real filters!</TextBlock>
</DataTemplate>

    public class FilterControlsTemplateSelector : DataTemplateSelector
{
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        var fe = container as FrameworkElement;
        if (fe == null) return null;

        return _GetDataTemplate(fe);
    }

    protected virtual DataTemplate _GetDataTemplate(FrameworkElement fe) {
        var template = fe.FindResource("defaultFilterContent") as DataTemplate;
        return template;
    }
}

Resources and Selector (calling Assembly)

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/Core.Presentation.Wpf;component/Themes/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>

<local:PimFilterControlsTemplateSelector x:Key="filterControlsTemplateSelector"/>

<DataTemplate x:Key="pimFilterContent">
    <Grid>
        <Border Style="{StaticResource FilterPanelBorderStyle}">
            <StackPanel Orientation="Horizontal" >
                <cc:SearchTextBox 
                        x:Name="stbLastNameFilter" Style="{StaticResource FilterPanelSearchTextBoxStyle}"
                        />
                <cc:SearchTextBox 
                        x:Name="stbFirstNameFilter" Style="{StaticResource FilterPanelSearchTextBoxStyle}"
                        />
            </StackPanel>
        </Border>

    </Grid>
</DataTemplate>


public class PimFilterControlsTemplateSelector : FilterControlsTemplateSelector
{

    protected override DataTemplate _GetDataTemplate(FrameworkElement fe)
    {
        var dc = fe.DataContext;
        if (dc == null) return null;

        DataTemplate result = null;
        if (dc is PimMasterVm)
        {
            result = fe.FindResource("pimFilterContent") as DataTemplate;
        }
        else {
            result = base._GetDataTemplate(fe);
        }
        return result;
    }

}

Application Dictionary setup (calling assembly)

    <Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/Parties.Presentation.Wpf;component/PimCommonResources.xaml" />                
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

</Application.Resources>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:41:19+00:00Added an answer on May 25, 2026 at 5:41 pm

    I gave up on making the DataTemplateSelector work and wound up doing the following:

    1. creating a FilterContentConverter
    2. adding a FilterContentKey (string) to my view model

    The converter just takes the FilterContentKey and does a resource lookup to get the DataTemplate with that key. This winds up being nicely testable, and even better – it works!

    Solution code below, with thanks to Vladamir Dorokhov and this SO answer for helping me get the ContentControl binding right.

    HTH,
    Berryl

    Filtering Control

    enter image description here

    <Grid>
        <Border Style="{StaticResource FilterPanelBorderStyle}">
            <StackPanel Orientation="Horizontal" x:Name="myFilterPanel" >
                <ContentControl x:Name="ctrlFilters" 
                                ContentTemplate="{Binding Path=FilterContentKey, Converter={StaticResource filterTemplateContentConv}}" />
                <Button x:Name="btnClearFilter" Style="{StaticResource FilterPanelClearButtonStyle}" />
                <Label x:Name="lblStatus" Style="{StaticResource FilterPanelLabelStyle}" Content="{Binding Status}" />
    
            </StackPanel>
        </Border>
    
    </Grid>
    

    Data Template (resource)

    <DataTemplate x:Key="pimFilterContent">
        <StackPanel Orientation="Horizontal" >
            <cc:SearchTextBox x:Name="stbLastNameFilter" 
                Style="{StaticResource FilterPanelSearchTextBoxStyle}"
                Text="{Binding Path=LastNameFilter, UpdateSourceTrigger=PropertyChanged}" 
                            />
            <cc:SearchTextBox x:Name="stbFirstNameFilter" 
                Style="{StaticResource FilterPanelSearchTextBoxStyle}"
                Text="{Binding Path=FirstNameFilter, UpdateSourceTrigger=PropertyChanged}" 
                            />
        </StackPanel>
    </DataTemplate>
    

    Converter

    /// <summary>
    /// Thin wrapper around a resource lookup designed to result in a <see cref="DataTemplate"/> 
    /// representing filering controls.
    /// </summary>
    [ValueConversion(typeof(object), typeof(DataTemplate))]
    public class FilterTemplateContentConverter : IValueConverter
    {
        public const string DEFAULT_CONTENT = "undefinedFilterContent";
        protected readonly ResourceLocator _resourceLocator;
    
        /// <summary>
        /// Initializes a new instance of the <see cref="FilterTemplateContentConverter"/> class.
        /// Unit tests can use this to pass in an app for the <see cref="ResourceLocator"/>.
        /// </summary>
        /// <param name="app">The app.</param>
        public FilterTemplateContentConverter(Application app) { _resourceLocator = new ResourceLocator(app); }
    
        /// <summary>
        /// Initializes a new instance of the <see cref="FilterTemplateContentConverter"/> class.
        /// The 'real' application uses this.
        /// </summary>
        public FilterTemplateContentConverter()
        {
            _resourceLocator = new ResourceLocator();
        }
    
        public virtual object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            var key = value as string;
            return _resourceLocator.GetResource(key ?? DEFAULT_CONTENT);
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a recursive method on the base form that takes in a control
I have user control that inherits a base control class and these user controls
I have a custom base user control in silverlight. <UserControl x:Class=Problemo.MyBaseControl xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml xmlns:d=http://schemas.microsoft.com/expression/blend/2008
I'm trying to skin HTML output which I don't have control over. One of
Is there a way to Minimize an external application that I don't have control
I have an application with plugin-like structure. all application forms inherit from a base
I have a user control...and the base page(s) which uses this user control has
I have a control that I purchased. When I tried to inherit from the
I have a control derived from ComboBox , I want to use the ComboBox
I have subclassed the RadioButtonList control in order to create a Control Adapter that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.