我已经在代码后面创建了一个按钮,现在需要使用xaml中的样式,我该如何在c#中调用这个样式来应用于我的按钮?
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += OnLoaded;
}
private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
var button1 = new Button();
TestPanel.Children.Add(button1);
}
}
}
样式是 <Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
这个标记定义了一个名为 "ButtonStyle" 的样式,它的目标类型是按钮。