0

我在 xaml 中有一个控制模板,目标是带有 x:Key = "NewBtn" 的 ToogleButton

如果在 xaml 代码中这是代码,请帮助我使用控制模板中的模板在 C# 代码中创建一个工具按钮:

<ToggleButton Template="{DynamicResource NewBtn}" 
                  Margin="12,21,0,0" HorizontalAlignment="Left" Width="151" Height="29" VerticalAlignment="Top"
                  x:Name="newBtn"
                 Checked = newBtn_Checked Unchekcked = newBtn_Unchecked
                  />

请帮助我如何在 C# 中创建它

4

1 回答 1

2
var button = new ToggleButton
{
    Margin = new Thickness(12, 21, 0, 0),
    HorizontalAlignment = HorizontalAlignment.Left,
    Width = 151,
    Height = 29,
    VerticalAlignment = VerticalAlignment.Top,
    Name = "newBtn",
};
button.SetResourceReference(Button.TemplateProperty, "NewBtn");

or

ToggleButton button = new ToggleButton();
button.Margin = new Thickness(12, 21, 0, 0);
button.HorizontalAlignment = HorizontalAlignment.Left;
button.Width = 151;
button.Height = 29;
button.VerticalAlignment = VerticalAlignment.Top;
button.Name = "newBtn";
button.SetResourceReference(Button.TemplateProperty, "NewBtn");
于 2011-04-15T06:15:00.367 回答