wpf 以编程方式添加动态资源 C#

发布于 2024-11-01 16:40:14 字数 482 浏览 2 评论 0原文

我在 xaml 中有一个 controltemplate,目标是 ToogleButton,其中 x:Key = "NewBtn"

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

<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# 中创建它

i have a controltemplate in xaml and the target is ToogleButton with x:Key = "NewBtn"

please help me create a toogle button in C# code using template from the Control Template
if in xaml code this is the code:

<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
                  />

please help me how to create it in c#

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

韶华倾负 2024-11-08 16:40:14
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");

或者

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");
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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文