将自定义命令设置为以编程方式创建的按钮(Set Custom Commands to programmatically created Buttons)

编程入门 行业动态 更新时间:2024-10-19 06:25:43
自定义命令设置为以编程方式创建的按钮(Set Custom Commands to programmatically created Buttons)

我是WPF的新手,并尝试创建一个从XML文件构建自己的接口。 新构建的界面包含不同的控件,如文本框,标签或按钮。 最后一个让我头疼几天,因为每个按钮都会得到自己的命令。

按钮的示例XML代码:

ID="1001" GroupID="1" Control="Button" Content="Apply" Margin="2" Width="60"

这是定义每个按钮的方法:

public Button Button(XElement xElement, Button newButton) { if (xElement.Attribute("Width") != null) newButton.Width = Convert.ToDouble((xElement.Attribute("Width").Value)); if (xElement.Attribute("Content") != null) { string sContent = xElement.Attribute("Content").Value; //Here gets the button its command Commands Commands = new Commands(); Commands.setCommand(sContent, newButton); newButton.Content = sContent; } return newButton; }

内容属性同时命名按钮和功能。

问题 :基本上我尝试构建一个包含所有可能命令的命令类。 每次创建按钮时, setCommand(sContent, newButton)方法都会扫描commands类,并通过Switch-Case语句将匹配命令设置为此按钮。

现在,每次单击按钮时,都会触发分配的命令。 但这样做甚至可能做到这一点,还是我走错了路? 有没有更简单的解决方案来做到这一点,或者我只是缺少命令绑定的基本要素?

任何暗示是赞赏。

I'm new to WPF and try to creat an interface that builds itself from a XML file. The new built interface contains different controls like textboxes, labels or buttons. And the last one gives me headaches for days now, because every button shall get its own command.

Example XML Code for buttons:

ID="1001" GroupID="1" Control="Button" Content="Apply" Margin="2" Width="60"

And this is the method that defines each button:

public Button Button(XElement xElement, Button newButton) { if (xElement.Attribute("Width") != null) newButton.Width = Convert.ToDouble((xElement.Attribute("Width").Value)); if (xElement.Attribute("Content") != null) { string sContent = xElement.Attribute("Content").Value; //Here gets the button its command Commands Commands = new Commands(); Commands.setCommand(sContent, newButton); newButton.Content = sContent; } return newButton; }

The Content Attribute names the button and the function at the same time.

Problem: Basically I try to build a commands class that contains all possible commands. Each time a button is created the setCommand(sContent, newButton) method scans the commands class and sets up the matching command to this button via a Switch-Case statement.

And now each time the button is clicked the assigned command should fire. But is it even possible to do it this way, or am I on the wrong track? Is there an easier solution to do this or am I just missing essentials of command binding?

Any hint is appreciated.

最满意答案

首先,如果您定义了自己的命令,并希望绑定到UI上的按钮,那么它们应该通过public properties in your VM or DataContext的public properties in your VM or DataContext 。

然后,一旦你有这样的安排,你可以设置如下按钮上的命令:

Binding binding = new Binding(); binding.Path = new PropertyPath("MyCommand"); //Name of the property in Datacontext button.SetBinding(Button.CommandProperty, binding);

First of all, if you have your own commands defined that you want to bind to Buttons on your UI, then they should be exposed via public properties in your VM or DataContext of your UI.

Then once you have this arrangement in place, you can set the Command on button like below:

Binding binding = new Binding(); binding.Path = new PropertyPath("MyCommand"); //Name of the property in Datacontext button.SetBinding(Button.CommandProperty, binding);

更多推荐

本文发布于:2023-04-28 05:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1330315.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   设置为   按钮   命令   方式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!