如何检测动态创建的绑定的发送者(按钮)

发布于 2024-12-05 20:52:18 字数 857 浏览 0 评论 0原文

我动态创建一些 RibbonButton,并根据 xml 文件将它们添加到组中。以下功能的执行次数与 xml 文件中找到的条目一样多。

private void ExtAppsWalk(ExternalAppsXml p, AppsWalkEventArgs args)
    {

            RibbonButton rBtn = new RibbonButton();
            rBtn.Name = args.Name;
            Binding cmdBinding = new Binding("ExtAppCommand");
            rBtn.SetBinding(RibbonButton.CommandProperty, cmdBinding);

            Binding tagBinding = new Binding("UrlTag");
            tagBinding.Mode = BindingMode.OneWayToSource;
            rBtn.SetBinding(RibbonButton.TagProperty, tagBinding);

            rBtn.Label = args.Haed;
            rBtn.Tag = args.Url;

            rBtn.Margin = new Thickness(15, 0, 0, 0);

            MyHost.ribGrpExtern.Items.Add(rBtn);

    }

我尝试使用 Tag 属性来存储单击相应按钮时启动的 Url。不幸的是,与 Tag 属性的绑定只提供了最后插入的 URL。 确定按下哪个按钮或更新 Tag 属性的最佳方法是什么。

I create some RibbonButtons dynamically and add them to a group according to an xml file. The follwoing function is carried out as often as entries found in the xml file.

private void ExtAppsWalk(ExternalAppsXml p, AppsWalkEventArgs args)
    {

            RibbonButton rBtn = new RibbonButton();
            rBtn.Name = args.Name;
            Binding cmdBinding = new Binding("ExtAppCommand");
            rBtn.SetBinding(RibbonButton.CommandProperty, cmdBinding);

            Binding tagBinding = new Binding("UrlTag");
            tagBinding.Mode = BindingMode.OneWayToSource;
            rBtn.SetBinding(RibbonButton.TagProperty, tagBinding);

            rBtn.Label = args.Haed;
            rBtn.Tag = args.Url;

            rBtn.Margin = new Thickness(15, 0, 0, 0);

            MyHost.ribGrpExtern.Items.Add(rBtn);

    }

I tried to use the Tag property to store the Url's to be started when the respective button is clicked. Unfortunately the binding to the Tag property gives me the last inserted Url only.
What would be the best way to figure out which button is hit or to update the Tag property.

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

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

发布评论

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

评论(1

烙印 2024-12-12 20:52:18

默认情况下,数据上下文是 Viewmodel 的上下文。添加按钮的 RibbonGroup 是在设计时在 xaml 文件中创建的。我使用该构造:

MyHost.ribGrpExtern.Items.Add(rBtn);

添加按钮。它可能并不真正符合 mvvm 模式。可能其他人有更好的想法来实现这一点。

我找到了解决问题的方法 此处并使用 RelayCommand 类。所以我可以将对象(我的 Url)传递给 CommandHandler。

RibbonButton rBtn = new RibbonButton();

rBtn.Name = args.Name;
Binding cmdBinding = new Binding("ExtAppCommand");
rBtn.SetBinding(RibbonButton.CommandProperty, cmdBinding);
rBtn.CommandParameter = (object)args.Url;



private void ExtAppFuncExecute(object parameter)
{

 if (parameter.ToString().....//myUrl

The datacontext is by default the context of the Viewmodel. The RibbonGroup to which the Buttons are added is created in the xaml file at designtime. I use that construct:

MyHost.ribGrpExtern.Items.Add(rBtn);

to add the buttons. It maight not really be conform with the mvvm pattern. May be someone else has a better idea to carry that out.

I foud a solution for my problem here and use the RelayCommand class. So I can pass objects (my Url) to the CommandHandler.

RibbonButton rBtn = new RibbonButton();

rBtn.Name = args.Name;
Binding cmdBinding = new Binding("ExtAppCommand");
rBtn.SetBinding(RibbonButton.CommandProperty, cmdBinding);
rBtn.CommandParameter = (object)args.Url;



private void ExtAppFuncExecute(object parameter)
{

 if (parameter.ToString().....//myUrl
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文