locked
Can't use StaticResource as Content of ContentControl RRS feed

  • Question

  • I have the following code where i am trying to use Resource "MyPanel1"(StackPanel) as Content in the ContentControl "MyContentControl1" but when i run it i get the error "Error HRESULT E_FAIL has been returned from a call to COM component". IS there something wrong with the below code. HOw can i fix this issue ??

     

    <UserControl x:Class="CollapsingGridSplitter.TestControl"
    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"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
     <Grid x:Name="LayoutRoot" Background="White">
      <Grid.Resources>
       <StackPanel x:Key="MyPanel1" Name="MyPanel1" />
       <sys:String x:Key="MyString1">MyString1</sys:String>
       <ControlTemplate x:Name="MyTemplate1" TargetType="Button">
        <Grid>
         <ContentControl Content="{StaticResource MyPanel1}" Name="MyContentControl1"/>
        </Grid>
       </ControlTemplate>
      </Grid.Resources>
      <Button Content="MyButton" Template="{StaticResource MyTemplate1}"/>
     </Grid>
    </
    UserControl>

    Wednesday, May 11, 2011 11:47 AM

Answers

  • Hi,

    If you want to get reference in a content control or other controls, you can use VisualTreeHelper and rescurion to find it. Here is a rescurion method which could find the element according to the name.

    private object FindElement(DependencyObject parent, string name)
    {
        if (parent is FrameworkElement)
        {
            object result = ((FrameworkElement)parent).FindName(name);
            if (result == null)
            {
                int childrencount = VisualTreeHelper.GetChildrenCount(parent);
                for (int i = 0; i < childrencount; i++)
                {
                    result = FindElement(VisualTreeHelper.GetChild(parent, i), name);
                    if (result != null)
                        break;
                }
                return result;
            }
            else
            {
                return result;
            }
        }
        else
        {
            return null;
        }
    }
    


     

    Thursday, May 12, 2011 10:07 PM

All replies

  • Content cannot be a resource. It should be either a control or a text

    Wednesday, May 11, 2011 11:23 PM
  • Thanks Neal.

    My issue is actually to get hold of the StackPanel i put inside the ControlTemplate "MyTemplate1", so that i can add another controls inside it at the time of loading my own UserControl or even at runtime. But i dont see any way get refrence to anything i put inside ControlTemplate. I tried searching and found i can use either VisualTreeHelper.GetChild() method or FrameworkElement.findName method but both are not working for me.

    I also tried to store the refrences at StackPanel Loaded event but it is getting invoked after the UserControl Loaded event, i can probably work with it but wanted to know if there is better solution or is there a way to get refrence to it before it's loaded event.

     

    Thanks,

    Tarun Ramsighani

    Thursday, May 12, 2011 7:42 AM
  • I think you will need to change template control at run time in this case. You can't access your template and a control in it, but you can surely change template which should the control which you wanted to get added.

    Thursday, May 12, 2011 8:09 AM
  • Hi,

    If you want to get reference in a content control or other controls, you can use VisualTreeHelper and rescurion to find it. Here is a rescurion method which could find the element according to the name.

    private object FindElement(DependencyObject parent, string name)
    {
        if (parent is FrameworkElement)
        {
            object result = ((FrameworkElement)parent).FindName(name);
            if (result == null)
            {
                int childrencount = VisualTreeHelper.GetChildrenCount(parent);
                for (int i = 0; i < childrencount; i++)
                {
                    result = FindElement(VisualTreeHelper.GetChild(parent, i), name);
                    if (result != null)
                        break;
                }
                return result;
            }
            else
            {
                return result;
            }
        }
        else
        {
            return null;
        }
    }
    


     

    Thursday, May 12, 2011 10:07 PM