我正在创建一个从 ItemsControl 派生的自定义控件,现在我遇到了一个问题。当我尝试在示例中设置控件的背景时,它不起作用。下面是控件的代码:
<!--Parent-->
<Style TargetType="{x:Type local:Parent}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:Parent}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ScrollViewer>
<ItemsPresenter/>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--MainChild-->
<Style TargetType="{x:Type local:MainChild}">
<Setter Property="Height" Value="430"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MainChild}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ItemsPresenter />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--InnerChild-->
<Style TargetType="{x:Type local:InnerChild}">
<Setter Property="Width" Value="100"/>
<Setter Property="Height" Value="100"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:InnerChild}">
--------------------------------------
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我的自定义控件的基本控件是
Parent-ItemsControl MainChild-ItemsControl InnerChild-ContentControl
在我的示例中,我尝试将背景设置如下:
<local:Parent Background="Yellow" >
<local:MainChild Background="Green">
<local:InnerChild Content="Item1" Background="#FF008C00"/>
</local:MainChild>
</local:Parent>
对于从 ItemsControl 派生的两个元素,背景属性都不起作用。
欢迎任何建议。