在后面的代码中查找类型的默认样式

Ton*_*ile 10 wpf code-behind wpf-style

在WPF中,您可以创建一个Style充当XAML中控件类型的默认值:

<Style TargetType="{x:Type local:MyControl}">
    . . .
</Style>
Run Code Online (Sandbox Code Playgroud)

然后,当WPF显示该控件时,它会Style根据其类型从资源中查找该控件.

我想在我的程序的代码隐藏中做相同的操作.我怎么找到的Style

小智 24

您可以使用控件类型作为键来搜索应用程序级资源中的样式:

Style defaultStyle = Application.Current.TryFindResource(typeof(MyControl)) as Style;
Run Code Online (Sandbox Code Playgroud)