0

我正在开发一个从ItemsControl. 在 generic.xaml 文件中,我为该控件创建了样式并定义了一个ItemTemplate

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

我想将in 的Background属性绑定到.BorderDataTemplateMyItemsControl

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

没有任何效果。我在这里想念什么?

4

1 回答 1

3

DependencyProperty 的类型是什么?是Brush还是string?这个简单的代码对我有用:

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

只是为了在这里进行测试,我绑定到 ItemsControl 的 Name 属性,即“黄色” - 它可以工作。

于 2013-08-30T07:28:58.103 回答