如何查找在WPF中在运行时中创建的动态控件 [英] how to find the dynamic controls that are created in runtime in wpf

查看:77
本文介绍了如何查找在WPF中在运行时中创建的动态控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我第二次提出的问题.

我已经在运行时创建了文本框和组合框.

我想找到该控件的名称,并且只想在运行时使用它的属性.

是否可以在该特定时间找到控件的名称?

我无法使用find方法.任何人都可以清楚地解释吗?

谢谢您的关注.
在这里,我将对我所做的代码进行一些示例;

Hi,

This is a question that I am raising for a second time.

I had created the Textboxs and comboboxs in runtime.

I want to find that control''s name and I want to use its properites at run-time only.

Is it possible to find the name of the controls at that particular instant of time?

I am not able to acess the find method. Can anyone explain clearly?

Thank you for your attention.
here i am going to do some sample the code that what i had done;

Canvas a = new Canvas();
               a.Height = 30;
               a.Width = 715;
               a.Name = "Pan" + i.ToString();
               a.Background = Brushes.PowderBlue;
               a.Margin = new Thickness(3, t + height, 0, 0);
               if (i >= 2)
               {
                   productspan.Height = productspan.Height + 40;
               }
               productspan.Children.Add(a);
               height = height + 40;
               Label l1 = new Label();
               l1.Content = "Item Name";
               l1.Margin = new Thickness(5, 5, 0, 0);
               a.Children.Add(l1);
               ComboBox cb = new ComboBox();
               cb.Width = 120;
               cb.Name = "cbsitems" + i.ToString();
               OleDbCommand cmd = new OleDbCommand("select * from stocktable where remain >=0 order by remain desc", con);
               OleDbDataReader dr;
               connect();
               dr = cmd.ExecuteReader();
               while (dr.Read())
               {
                   if (dr["item"].ToString() != "New")
                   {
                       cb.Items.Add(dr["item"].ToString());
                   }
               }
               cb.Items.Add("Select");
               cb.SelectedItem = "Select";
               diconnect();
              cb.SelectionChanged += new SelectionChangedEventHandler(cb_SelectionChanged);
               cb.Margin = new Thickness(70, 5, 0, 0);
               a.Children.Add(cb);

               Label l3 = new Label();
               l3.Content = "Amount/item";
               l3.Margin = new Thickness(200, 5, 0, 0);
               a.Children.Add(l3);
               TextBox tx1 = new TextBox();
               tx1.IsReadOnly = true;
               tx1.Margin = new Thickness(275, 5, 0, 0);
               tx1.Width = 100;
               tx1.GotFocus += new RoutedEventHandler(tx1_GotFocus);
               tx1.Name = "pritxt" + i.ToString();
               a.Children.Add(tx1);



这里首先创建

标签-....
combobox-"cbsitems" + i.ToString();
标签-....
textbox-"pritxt" + i.ToString();

我的要求是,当comobobox选择更改时,所选项目的价格应在文本框中输入;我能够从表格中检索价格,而且我还面临将价格放入文本框(动态创建)的问题
我想现在您可以理解我的问题了.



here first am creating

label-....
combobox-"cbsitems" + i.ToString();
label-....
textbox-"pritxt" + i.ToString();

my requirement is that when the comobobox selection changed , then selected item price should be apper in the textbox; iam able to retrive the price from the table, more over i am facing the problem with placing the price in the textbox(which was created dynamically )
i think right now u can understand my problem

推荐答案

我不太了解.您创建了文本框和组合框
在运行时,因此您已经有了对它们的引用(可以将其存储
在您选择的任何方便的数据结构/对象中.
您要达到的目标是什么?
显示一些代码(最多简化为要点)来说明
您的问题.

干杯

曼弗雷德(Manfred)
I don''t quite understand. You created the textboxes and comboboxes
at runtime so you already have a reference to them (that could be stored
in any convenient data structure / object of your choosing).
What is is you''re trying to achieve?
Show some code (simplified to the essentials at best) to illustrate
your issues.

Cheers

Manfred


嗨sivakumarmr10,

您如何添加控件?如果您使用的是WPF,则有可能将"ContentPresenter"绑定到"CodeBehind"文件或"ViewModel"类中的列表,具体取决于您的方法,然后遍历该列表,挑选出您要使用的控件"重新寻找.

您创建的列表可以是您自己的预定义类型的列表,然后此类型可以具有诸如唯一标识符之类的属性.遍历此列表时,可以根据该属性检索所需的控件.

您可以在新创建类型的数据模板中定义控件的类型.

Hi sivakumarmr10,

How are you adding your controls? If you''re using WPF it may be possible to bind a "ContentPresenter" to a List in your "CodeBehind" file or "ViewModel" class, depending on your methodology and then iterate through that list, picking out the control you''re looking for.

The list that you create, could be a list of your own predefined type, this type could then have a property such as unique identifier. When iterating through this list, you could retrieve the control that you need depending on that attribute.

You can define the type of control inside a data-template of your newly created type.

<DataTemplate DataType="{x:Type MyClass}">
    <Button/>
<<DataTemplate />



然后在您的XML文件中:



Then in your XML file :

<Border BorderThickness="1" Background="White">
    <ScrollViewer>
        <ItemsControl ItemsSource="{Binding MyClassDP}" />
    </ScrollViewer>
</Border>



然后,根据后面代码的数据上下文,您可能希望在后面的代码中或单独的类中设置依赖项属性,以不使用.xaml.cs文件:



Depending on the data context of your code behind you may then wish to set a dependency property in either the code behind or a separate class, naive to your .xaml.cs file :

#region MyClassDp
/// <summary>
/// MyClassDp Dependency Property
/// </summary>
public static readonly DependencyProperty MyClassDpProperty =
    DependencyProperty.Register("MyClassDp", typeof(List<MyClass>), typeof(Program),
        new FrameworkPropertyMetadata((List<MyClass>)null));
/// <summary>
/// Gets or sets the MyClassDp property. This dependency property
/// indicates a list of my custom class.
/// </summary>
public List<MyClass> MyClassDp
{
    get { return (List<MyClass>)GetValue(MyClassDpProperty); }
    set { SetValue(MyClassDpProperty, value); }
}
#endregion




我希望这会有所帮助,如果这不是您要采用的方法,则深表歉意.对于WPF数据绑定,我在其他地方也对此给出了类似的答案,这可能会有所帮助.

将DP自动生成的代码片段归功于 DR WPF .




I hope this helps, apologies if that''s not the approach you''re going for. I have given a similar answer to this one elsewhere in relation to WPF data-bindings which might help.

Credit to DR WPF for DP Auto-Generated Snippet.


这篇关于如何查找在WPF中在运行时中创建的动态控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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