0

So I have this custom control library consisting of two controls. For some reason, I keep getting errors like "cannot bind properties of ContentPresenter because there is no property named 'Content' on type..." and "the member 'template' is not recognized or is not accessible." All my code seems in line and even after searching google (a hundred times over), I have not been able to figure out what's preventing my solution to build.

Wasted an hour trying to figure this out and I still got nothing. Here's the code:

Generic.xaml

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Imagin.Controls">
<Style TargetType="{x:Type local:WorkSpace}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:WorkSpace}">
                <ContentPresenter ContentSource="Content" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style TargetType="{x:Type local:Tile}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:Tile}">
                <ContentPresenter ContentSource="Content" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
</ResourceDictionary>

Tile.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Imagin.Controls
{

    public class Tile : Border
    {

        public string Title { get; set; }
        public bool IsClosable { get; set; }
        public bool IsLockable { get; set; }
        public Tiles TileType { get; set; }
        public Orientations Orientation { get; set; }

        static Tile()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(Tile), new FrameworkPropertyMetadata(typeof(Tile)));
        }

    }

}

WorkSpace.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Imagin.Controls
{

    public enum Orientations
    {
        Top,
        TopMiddle,
        Middle,
        BottomMiddle,
        Bottom,
        Left,
        LeftMiddle,
        RightMiddle,
        Right
    }

    public enum Tiles
    {
        Window,
        Tabbed,
        Column
    }

    public class WorkSpace : Grid
    {
        static WorkSpace()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(WorkSpace), new FrameworkPropertyMetadata(typeof(WorkSpace)));
        }
    }
}

Any suggestions?

  • I don't understand how Workspace which inherits from Grid even has a Template property ? it does not inherit from Control class .. I would prove this using Blend but sadly i don't have a working copy of blend on this machine. maybe tomorrow.. Further more lets say it was some control. maybe it does not have a Content property and that's what the error refers to. – eran otzap Oct 11 '14 at 00:03
  • Basically, I'm trying to make WorkSpace a grid-like control that can nest child controls ("Tiles", which have border-like functionalities), and am using the template property to restructure what elements are to be displayed. In my main project, I've re-templated the same such objects already without a problem. Or is this kind of thing not allowed? I am still somewhat of a beginner when it comes to creating custom controls. This is actually the first I've attempted. –  Oct 11 '14 at 00:09
  • Grid is not a Control it is a FrameworkElement. All controls are FrameworkElements but not all FrameworkElement's are Controls , only Controls can be templated. – eran otzap Oct 11 '14 at 00:12

2 Answers2

3

Looks like the answer to my question was simple after falling upon this article: Adding children to UserControl

All I needed was to change the base class to ContentControl, which allows access to the template property and insertion of child controls.

Community
  • 1
  • 1
0

this happened to me because i had the derived class set to partial i.e had this

   public partial class ValueTypeMatrix: Control

should have had this

public class ValueTypeMatrix: Control
dss539
  • 6,804
  • 2
  • 34
  • 64
Declan Taylor
  • 408
  • 6
  • 8