在事件中从xaml传递控件名称,然后在后面的代码中操纵该控件? [英] Pass a control name from xaml in an event and then manipulate the control in the code behind?

查看:73
本文介绍了在事件中从xaml传递控件名称,然后在后面的代码中操纵该控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢大家对我之前的问题的帮助.当人们学到一些东西时,这些文章开始变得越来越有意义,这真是令人惊讶.

自从我不断进步以来,我不再想踢猫了(我喜欢猫,只是一个比喻).但是,有一种简单的方法可以传递控件名称,然后在后面的代码中使用它,即:

Test.XAML

< ComboBox名称="cmbStatus" DropDownOpened ="cmbControl_Open(" cmbStatus)" ...

然后在Test.XAML.cs中,我想执行以下操作:

私有void cmbControl_Open(string strControlName)
{
combobox FoundBox = FindName(strControlName);
FoundBox.DisplayMemberPath =字段";

我看到很多有关获取集合然后处理集合中每个成员的文章.是否有更直接的解决方案?

我没有说清楚.基本上,我有10个组合框,每个组合框都调用一个查找表.我想创建一个常规事件,并将其传递给组合框,查询,显示路径和值路径的名称.然后,在子例程中,当组合框下降时,在domainsource中进行传播.

这样,我有一个事件可以处理所有组合框.

谁能告诉我如何回复?我只是点击答案吗?

Larry

Thanks for everyone''s help on my prior questions. It''s amazing how when one learns something the articles start to make more and more sense.

I no longer feel like kicking the cat (I like cat''s, just a figure of speech) since I am making progress. However is there a simple way to pass the name of a control and then use it in the code behind ie:

Test.XAML

<ComboBox Name="cmbStatus" DropDownOpened="cmbControl_Open("cmbStatus")" ...

Then in the Test.XAML.cs I want to do something like:

private void cmbControl_Open(string strControlName)
{
combobox FoundBox = FindName(strControlName);
FoundBox.DisplayMemberPath = "Field";

I see lots of articles on getting a collection and then processing each member of the collection. Is there a more direct solution to the problem?

I am not making myself clear. Basically I have 10 comboboxes that each call a lookup table. I wanted to create a general event and pass it the name of the combobox, query, displaypath and valuepath. Then in the subroutine propogate the domainsource when the combobox drops down.

That way I have one event handling all of the combo boxes.

Can anyone tell me how to reply? Do I just hit answer?

Larry

推荐答案

您不能在这样的事件处理程序上指定参数.

由于您知道哪个控件发布了该事件,因此您可以简单地在事件处理程序中将该控件还原为该控件.

You can''t specify parameters on event handlers like that.

Since you know what control posted the event, you can simply rerfer to that control in the event handler.

private void cmbControl_Open(string strControlName)
{
    this.cmbStatus.DisplayMemberPath = "Field";
}


拉里,

为什么要将控件名称作为参数传递?
您可以直接在事件处理程序中访问控件.

Hi Larry,

Why do you want to pass the control name as a parameter?
You can directly access the control inside your event handler.

protected void cmbControl_Open(object sender, EventArgs e)
{
    cmbStatus.DisplayMemberPath = "Field";
}



是的,如果您对多个ComboBox使用相同的事件处理程序,那么您也可以非常轻松地获得combobox.您会注意到,您的事件处理程序有一个发送者.这是您打开的指定组合框.

因此,在这种情况下,请编写以下代码:



Yup, if you are using the same event handler for multiple ComboBox, then also you can get the combobox very easily. You can notice that, your event handler has a sender. This is your specified combobox that you opened.

So, write the following code in that case:

protected void cmbControl_Open(object sender, EventArgs e)
{
    ComboBox myCombo = sender as ComboBox;
    myCombo.DisplayMemberPath = "Field";
}



如果此回答您的查询,请标记为答案".
感谢您在CodeProject上发布.



If this answers your query, please "Mark As Answer".
Thank you for posting at CodeProject.


这篇关于在事件中从xaml传递控件名称,然后在后面的代码中操纵该控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆