locked
Can't quite get DataTemplateSelector working

    Question

  • Here is my DataTemplateSelector header:

    #pragma once
    
    namespace DynamicSimulations
    {
    	public ref class ShapeDataTemplateSelector sealed : Windows::UI::Xaml::Controls::DataTemplateSelector
    	{
    	public:
    		property Windows::UI::Xaml::DataTemplate^ CubeTemplate
    		{
    			Windows::UI::Xaml::DataTemplate^ get() { return m_CubeTemplate;}
    			void set(Windows::UI::Xaml::DataTemplate^ value){ m_CubeTemplate = value;}
    		}	
    		
    		property Windows::UI::Xaml::DataTemplate^ SphereTemplate
    		{
    			Windows::UI::Xaml::DataTemplate^ get() { return m_Sphere;}
    			void set(Windows::UI::Xaml::DataTemplate^ value){ m_Sphere = value;}
    		}	
    		
    		property Windows::UI::Xaml::DataTemplate^ CylinderTemplate
    		{
    			Windows::UI::Xaml::DataTemplate^ get() { return m_Cylinder;}
    			void set(Windows::UI::Xaml::DataTemplate^ value){ m_Cylinder = value;}
    		}	
    
    		virtual Windows::UI::Xaml::DataTemplate^ SelectTemplateCore(Platform::Object^ item, Windows::UI::Xaml::DependencyObject^ container) override;
    	private:
    		Windows::UI::Xaml::DataTemplate^ m_CubeTemplate;
    		Windows::UI::Xaml::DataTemplate^ m_Sphere;
    		Windows::UI::Xaml::DataTemplate^ m_Cylinder;
    	};
    }


    Here is my implementation: 

    #include "pch.h"
    #include "ShapeDataTemplateSelector.h"
    #include "IShape.h"
    #include "Cube.h"
    #include "Sphere.h"
    #include "Cylinder.h"
    
    using namespace DynamicSimulations;
    
    using namespace Platform;
    using namespace Windows::UI::Xaml;
    
    
    DataTemplate^ ShapeDataTemplateSelector::SelectTemplateCore(Object^ item, DependencyObject^ container)
    {	
    	if(dynamic_cast<Cube^>(item))
    		return CubeTemplate;
    	else if(dynamic_cast<Sphere^>(item))
    		return SphereTemplate;
    	else if(dynamic_cast<Cylinder^>(item))
    		return CylinderTemplate;
    	else
    		return nullptr;
    }


    I haven't tested the dynamic cast thing, so it might not work, don't worry about that :)

    Here is the resources section of my xaml: 

        <Page.Resources>
            <DataTemplate x:Key="cubeTemplate">
                <StackPanel>
                    <StackPanel Width="180" Orientation="Horizontal">
                        <TextBlock Text="Height: " FontSize="20" VerticalAlignment="Bottom" Margin="10"/>
                        <TextBox Text="{Binding ElementName=lbShapes, Path=SelectedItem.Height, Mode=TwoWay}" Margin="10"/>
                    </StackPanel>
                    <StackPanel Width="180" Orientation="Horizontal">
                        <TextBlock Text="Width: " FontSize="20" VerticalAlignment="Bottom" Margin="10"/>
                        <TextBox Text="{Binding ElementName=lbShapes, Path=SelectedItem.Width, Mode=TwoWay}" Margin="10"/>
                    </StackPanel>
                    <StackPanel Width="180" Orientation="Horizontal">
                        <TextBlock Text="Depth: " FontSize="20" VerticalAlignment="Bottom" Margin="10"/>
                        <TextBox Text="{Binding ElementName=lbShapes, Path=SelectedItem.Depth, Mode=TwoWay}" Margin="10"/>
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
            <DataTemplate x:Key="sphereTemplate">
                <StackPanel>
                    <StackPanel Width="180" Orientation="Horizontal">
                        <TextBlock Text="Radius: " FontSize="20" VerticalAlignment="Bottom" Margin="10"/>
                        <TextBox Text="{Binding ElementName=lbShapes, Path=SelectedItem.Radius, Mode=TwoWay}" Margin="10"/>
                    </StackPanel>
                </StackPanel>
            </DataTemplate>        
            <DataTemplate x:Key="cylinderTemplate">
                <StackPanel>
                    <StackPanel Width="180" Orientation="Horizontal">
                        <TextBlock Text="Height: " FontSize="20" VerticalAlignment="Bottom" Margin="10"/>
                        <TextBox Text="{Binding ElementName=lbShapes, Path=SelectedItem.Height, Mode=TwoWay}" Margin="10"/>
                    </StackPanel>
                    <StackPanel Width="180" Orientation="Horizontal">
                        <TextBlock Text="Radius: " FontSize="20" VerticalAlignment="Bottom" Margin="10"/>
                        <TextBox Text="{Binding ElementName=lbShapes, Path=SelectedItem.Radius, Mode=TwoWay}" Margin="10"/>
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
            <local:ShapeDataTemplateSelector x:Key="shapeDataTemplateSelector"
                                             CubeTemplate="{StaticResource cubeTemplate}"
                                             SphereTemplate="{StaticResource sphereTemplate}"
                                             CylinderTemplate="{StaticResource cylinderTemplate}"/>
        </Page.Resources>


    And finally, this is how I call the ShapeDataTemplateSelector:

                        <ContentControl ContentTemplateSelector="{StaticResource shapeDataTemplateSelector}" />

    I've got all this in there, which is all you need to my knowledge. Everything builds and runs, but when the SelectTemplateCore method is never entered :/

    Thanks for the help!

    Friday, November 08, 2013 10:58 PM

Answers

  • The answer is quite simple. I had to change

    <ContentControl ContentTemplateSelector="{StaticResource shapeDataTemplateSelector}" />

    to: 


    <ContentControl Content="{Binding SelectedItem, ElementName=lbShapes}" ContentTemplateSelector="{StaticResource shapeDataTemplateSelector}" />

    Without binding the content control to something it won't ever select a template :)

    • Marked as answer by Carbaholic Wednesday, December 11, 2013 8:05 PM
    Wednesday, December 11, 2013 8:05 PM

All replies

  • Hi Carbaholic,

    Welcome to MSDN forum!

    From what you have said above, I couldn't reproduce your issue and I couldn't find where you have called the method yet.

    Have you tracked your code? Could you share with me more detailed info about the result you have collected.

    Please refer this link:

    http://msdn.microsoft.com/en-us/library/system.windows.controls.datatemplateselector(v=vs.110).aspx

    I hope it is helpful!

    Regards!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, November 11, 2013 9:47 AM
    Moderator
  • I'm sorry i've taken so long to reply. I got what I needed to do working using coverters so I haven't looked back at it since I asked the question.

    The funny thing is converters were behaving the same way, so I tried the template selector. Then I went back and tried converters and they started working. 

    Friday, November 15, 2013 8:11 PM
  • The answer is quite simple. I had to change

    <ContentControl ContentTemplateSelector="{StaticResource shapeDataTemplateSelector}" />

    to: 


    <ContentControl Content="{Binding SelectedItem, ElementName=lbShapes}" ContentTemplateSelector="{StaticResource shapeDataTemplateSelector}" />

    Without binding the content control to something it won't ever select a template :)

    • Marked as answer by Carbaholic Wednesday, December 11, 2013 8:05 PM
    Wednesday, December 11, 2013 8:05 PM