c# – 引用ControlTemplate内部的控件
作者:互联网
我如何在代码隐藏中形成我的构造函数,在下面的XAML中获取对OuterBorder控件的引用?
<Window Template="{DynamicResource WindowTemplate}">
<Window.Resources>
<ControlTemplate x:Key="WindowTemplate" TargetType="{x:Type Window}">
<AdornerDecorator>
<Border Name="OuterBorder" Background="Black" BorderBrush="Red" BorderThickness="1" CornerRadius="0">
<!-- Implementation here... -->
</Border>
</AdornerDecorator>
</ControlTemplate>
</Window.Resources>
</Window>
解决方法:
两种可能的解决方
解决方案1
在XAML中放置一个Loaded事件
<Border Name="OuterBorder" Loaded="Border_Loaded" ...
并在代码后面存储它在私人领域:
private Border border;
void Border_Loaded(object sender, RoutedEventArgs e)
{
this.border = (Border)sender;
}
要么:
解决方案2
覆盖窗口的OnApplyTemplate:
private Border border;
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
this.border = (Border) Template.FindName("OuterBorder", this);
}
标签:c,resources,wpf,controltemplate 来源: https://codeday.me/bug/20190621/1258119.html