2013-08-29 40 views
0

我正在开发从ItemsControl派生的自定义控件。在generic.xaml文件我创建了该控件的样式,也定义了ItemTemplate如何从样式中定义的DataTemplate绑定到TemplatedParent?

<Style TargetType="local:MyItemsControl"> 
    <Setter Property="ItemTemplate"> 
     <Setter.Value> 
      <DataTemplate> 
       <Border Background="Red"> 
        <!-- Other things in here --> 
       </Border> 
      </DataTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

我想将BorderDataTemplateBackground属性绑定到MyItemsControl的依赖项属性。

如果发现这里有几个问题,建议在绑定中使用MyItemsControl的元素名称,但只有在定义使用该控件的ItemTemplate时才起作用。我还尝试绑定到RelativeSource,将local:MyItemsControl定义为祖先类型。

没有工作。我在这里错过了什么?

+0

在ControlTemplate中可以使用TemplateBinding。但是这只能在ControlTemplate中使用......我会尝试祖先的方式。 – MVCible

+0

我尝试过 - 但无法让它正常工作。所以要么这不起作用 - 要么我犯了一个错误... – Spontifixus

回答

3

那个DependencyProperty的类型是什么?是Brush还是string? 这个简单的代码工作对我来说:

Background="{Binding Name, RelativeSource={RelativeSource AncestorType=ItemsControl}}" 

只是为了测试我在这里绑定到ItemsControl中的名称属性,它是“黄色” - 和它的作品。

相关问题