【发布时间】:2013-05-26 04:55:19
【问题描述】:
模板
<Style TargetType="{x:Type local:Viewport}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:Viewport}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ItemsPresenter/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<Canvas x:Name="PART_Canvas" IsItemsHost="True"/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
以及 OnApplyTemplate 中的代码
content = this.Template.FindName("PART_Canvas", this) as FrameworkElement;
内容总是返回null,为什么不起作用?
如果我用这个替换,程序直接退出
content = this.ItemsPanel.FindName("PART_Canvas", this) as FrameworkElement;
【问题讨论】:
-
在 OnApplyTemplate 方法中尝试类似 content = (Canvas)GetTemplateChild("PART_Canvas") 的方法,看看它是否有效。
-
@Sai GetTemplateChild 不起作用..画布在 ItemsPanel 我不知道如何访问它..
-
@sircodesalot
This,是从 ItemsControl 派生的自定义控件