WPF C# - 成员"模板"无法识别或无法访问?

时间:2014-10-10 23:41:54

标签: c# wpf windows xaml templates

所以我有这个由两个控件组成的自定义控件库。出于某种原因,我不断收到错误,例如"无法绑定ContentPresenter的属性,因为没有名为' Content'在类型..."和"会员模板'不被识别或无法访问。"我的所有代码似乎都符合要求,甚至在搜索谷歌(一百次)后,我还无法弄清楚是什么阻止了我的解决方案的构建。

浪费了一个小时试图解决这个问题,但我仍然一无所获。这是代码:

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)));
        }
    }
}

有什么建议吗?

2 个答案:

答案 0 :(得分:3)

在看到这篇文章之后,我的问题的答案看起来很简单: Adding children to UserControl

我只需要将基类更改为ContentControl,它允许访问模板属性和插入子控件。

答案 1 :(得分:0)

这发生在我身上,因为我将派生类设置为部分,即     有这个

   public partial class ValueTypeMatrix: Control

应该有这个

public class ValueTypeMatrix: Control