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 6809477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:05:29+00:00 2026-05-26T20:05:29+00:00

Consider the following part of a visual tree in a Windows Phone SL application:

  • 0

Consider the following part of a visual tree in a Windows Phone SL application:

Content presenter

As you can see, a PanoramaItem template contains a reference to a static DataTemplateSelector. It is a simple class that dynamically feeds the data template based on a provided Key, to display different views for different view models provided as DataContext to PanoramaItem. Here is the code for it:

public static T FindResource<T>(this DependencyObject initial, string key) where T : DependencyObject
        {
            DependencyObject current = initial;

            while (current != null)
            {
                if (current is FrameworkElement)
                {
                    if ((current as FrameworkElement).Resources.Contains(key))
                    {
                        return (T)(current as FrameworkElement).Resources[key];
                    }
                }

                current = VisualTreeHelper.GetParent(current);
            }

            if (Application.Current.Resources.Contains(key))
            {
                return (T)Application.Current.Resources[key];
            }

            return default(T);
        }
    }

    public class DataTemplateSelector : ContentControl
    {
        protected override void OnContentChanged(object oldContent, object newContent)
        {
            ContentTemplate = this.FindResource<DataTemplate>(newContent.GetType().FullName);
        }
    }

The problem is that I have no control over the creation of ContentPresenter you can see selected on an image above. To get a consistent layout, I need to be able to set it’s Vertical Alignment property. I don’t seem to know how I can do it, since I can’t reference this ContentPresenter. How can I set a ContentPresenter’s properties?

  • 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-26T20:05:30+00:00Added an answer on May 26, 2026 at 8:05 pm

    The solution yet again was simple:

    Define a style for my ContentControl derived class:

    <Style TargetType="support:DataTemplateSelector">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="support:DataTemplateSelector">
                            <ContentPresenter
                            ContentTemplate="{TemplateBinding support:DataTemplateSelector.ContentTemplate}"
                            Content="{TemplateBinding support:DataTemplateSelector.Content}" 
                            VerticalAlignment="Top"/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
    

    I have defined mine in UserControl.Resources section of a View’s XAML.

    The call the “restyling” line of code in the class’s constructor:

     public class DataTemplateSelector : ContentControl
        {
            public DataTemplateSelector()
            {
                this.DefaultStyleKey = typeof (DataTemplateSelector);
            } 
    

    And that’s how you can control the ContentPresenter element’s look of a ContentControl‘s derived control.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following hypothetical situation. As part of an application you are developing in
Consider the following declaration as part of SomeClass private Set<String> blah = new HashSet<String>();
Consider following code: My problem is: 1) I can't seem to cast the errors
Consider following program: static void Main (string[] args) { int i; uint ui; i
Consider the following setup: A windows PC with a LAN interface and a WiFi
Consider the following example: as part of configuration for my program the user supplies
I would like to point out a 'risky' part in F#. Consider the following
consider the following c++ code #include stdafx.h #include<iostream> using namespace std; this much part
here is part of tutorial in oracle page : Consider the following example: List
Consider the following function: template <typename A, typename B> auto Min(A&& a, B&& b)

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.