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

  • SEARCH
  • Home
  • 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 9298191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:15:40+00:00 2026-06-18T22:15:40+00:00

I’m creating a custom control and I’m trying to create partially specified template for

  • 0

I’m creating a custom control and I’m trying to create partially specified template for list box items. The template has some predefined parts and there should be another part that can be templated when using the control.

For this I have created a dependency property named SuggestionItemTemplate like so:

public static readonly DependencyProperty SuggestionItemTemplateProperty =
    DependencyProperty.Register("SuggestionItemTemplate", 
        typeof(DataTemplate), 
        typeof(AutoSuggestTextBox), 
        new PropertyMetadata(null));

In my custom controls’ generic.xaml I have:

<Style TargetType="local:AutoSuggestTextBox">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:AutoSuggestTextBox">
                <Grid>
                    <ListBox x:Name="ItemsControl">
                        <ListBox.ItemsPanel>
                            <ItemsPanelTemplate>
                                <StackPanel />
                            </ItemsPanelTemplate>
                        </ListBox.ItemsPanel>
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition Width="Auto" />
                                    </Grid.ColumnDefinitions>
                                    <ContentPresenter Grid.Column="0" 
                                                      ContentTemplate="{TemplateBinding SuggestionItemTemplate}" 
                                                      Content="{Binding}" />
                                    <ToggleButton Grid.Column="1" 
                                                  x:Name="DetailsHover" 
                                                  ClickMode="Hover" 
                                                  Style="{StaticResource DetailsToggleButtonStyle}" />
                                </Grid>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Unfortunatelly, this does not work as it’s not possible to use TemplateBinding from inside ContentPresenter nested into DataTemplate. (The member “SuggestionItemTemplate” is not recognized or is not accessible.)

I also tried to use ancestor binding (available in Silverlight 5) like:

<ContentPresenter Grid.Column="0" 
                  ContentTemplate="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=local:AutoSuggestTextBox}, Path=SuggestionItemTemplate}" 
                  Content="{Binding}" />

But this results in binding error:

Error: System.Exception: BindingExpression_CannotFindAncestor

I suppose this happens because I’m inside ControlTemplate of my custom control and “local:AutoSuggestTextBox” is not defined anywhere in the style.

The third option that I tried was to apply ContentTemplate in OnApplyTemplate override but this also doesn’t work:

var cp = itemsControlElement.ItemTemplate.LoadContent() as ContentPresenter;
cp.ContentTemplate = SuggestionItemTemplate;

In all cases, I get my grid with two columns, toggle button is visible but content presenter simple prints out view model’s type name. (I believe this is the default behavior if the ContentTemplate is null).

Is this even possible to do? Are there any other ways to specify a partial template and then only add customized template part when necessary?

As a workaround for now, I can specify

ItemTemplate="{TemplateBinding SuggestionItemTemplate}"

for the list box and then copy/paste the generic template everywhere I use this control. But this is the behavior I’m hoping to avoid in the first place.

Thanks!

edit: I used the code tags for all blocks of code, but they’re not highlighted for some reason. :/

  • 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-06-18T22:15:42+00:00Added an answer on June 18, 2026 at 10:15 pm

    I managed to solve this using a different approach. I used ancestor binding but instead of trying to reach the root control (my AutoSuggestTextBox) from the DataTemplate, I ask for a reference to my ListBox (here named ItemsControl).

    However, since the ListBox doesn’t have the SuggestionItemTemplate property, I sub-classed it to my own CustomListBox where I implemented that property. It all comes down to this code snippet:

    <Style TargetType="local:AutoSuggestTextBox">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="local:AutoSuggestTextBox">
            <Grid>
              <local:CustomizableListBox x:Name="ItemsControl"
                                         SuggestionItemTemplate="{TemplateBinding SuggestionItemTemplate}">
                <local:CustomizableListBox.ItemTemplate>
                  <DataTemplate>
                    <Grid>
                      <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                      </Grid.ColumnDefinitions>
                      <ContentPresenter Grid.Column="0" 
                                        ContentTemplate="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=local:CustomizableListBox}, Path=SuggestionItemTemplate}"
                                        Content="{Binding}" />
                      <ToggleButton Grid.Column="1" 
                                    x:Name="DetailsHover" 
                                    ClickMode="Hover" 
                                    Style="{StaticResource DetailsToggleButtonStyle}" />
                     </Grid>
                  </DataTemplate>
                </local:CustomizableListBox.ItemTemplate>
              </local:CustomizableListBox>
            </Grid>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace

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.