2009-08-21 45 views
0

我是使用ControlTemplate的新手。我正在写我的第一个控件,但我有(在我看来)一个非常奇怪的问题。TemplateBinding不能使用.NET框架对象

任何依赖属性,我让TemplateBinding工作,但是从.NET Framework对象即一个ContentControlContent财产或ItemsControlItems财产的任何属性没有得到在运行时填充。

我相信我失去了一些东西...只是它是什么,我不知道......

代码的下面是一个例子:

类是目前十分简单:

public class Title : ContentControl 
{ 
} 

和模板是:

<Style TargetType="{x:Type UI:Title}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type UI:Title}"> 
       <TextBlock Text="{TemplateBinding Content}" /> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

基础ContentControl CL ass是位于System.Windows.Controls.Control命名空间中的.NET类。

感谢,

亚当

+0

您可以添加代码片段以显示哪些不起作用? – Bubblewrap 2009-08-21 14:34:57

+0

对不起。我现在加了一个。 谢谢。 – Adam 2009-08-21 14:51:56

回答

1

我相信如果您想重写Content的放置位置,您可以使用ContentPresenter来做到这一点。

<Style TargetType="{x:Type UI:Title}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type UI:Title}"> 
       <Label> 
        <ContentPresenter /> 
       </Label> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

注意我也从一个TextBlock变为一个标签,因为我相信TextBlock.Text属性不会从ContentControl.Content接受。下面是我按照预期工作的一个示例:

<Window x:Class="ContentControlTest.Window1" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:ContentControlTest" 
     Title="Window1" Height="300" Width="300"> 
    <Window.Resources> 
     <Style TargetType="{x:Type local:Title}"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type local:Title}"> 
         <Button> 
          <ContentPresenter /> 
         </Button> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Window.Resources> 
    <local:Title> 
     <TextBlock Text="Happy Days!" /> 
    </local:Title> 
</Window> 
+0

我刚刚尝试了您的建议。它似乎没有工作...我已经做了更多的调查,似乎无论何时设置属性它不会显示,但如果我把一个值的Title类的构造函数赋值给内容属性,然后显示... – Adam 2009-08-21 15:24:10

+0

任何想法?感谢您的反馈意见。 – Adam 2009-08-21 15:24:51

+0

我已经更新了一个工作示例。 – user7116 2009-08-21 15:30:19

0

您可能需要实现您的收藏在你的对象和INotifyCollectionChanged INotifyPropertyChanged接口。