在WPF中如何在按钮事件处理程序中读取动态文本框的值 [英] In WPF How to read the value of Dynamic Textboxes in a button event Handler

查看:157
本文介绍了在WPF中如何在按钮事件处理程序中读取动态文本框的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

在运行时,iam在WPF窗口的画布上绘制多个形状(路径,矩形,椭圆形)并将形状信息存储在列表中
Iam试图做的是当我右键单击任何形状时,我想显示一个属性窗口,该属性窗口在属性窗口上显示该形状的细节,例如
高度:50
宽度:80
位置X:80 Y:100
....
... etc
为此,我创建了动态添加到wrapPanel的标签和文本框

Hello All,

During runtime iam drawing multiple shapes(path,rectangle,ellipse) on the canvas on a WPF Window and storing the shapes information in a List
What iam trying to do is when i right click on any shape, i want to display a property window which show the deatils of that shapeon the propert window like
Height: 50
width: 80
Position X: 80 Y: 100
....
...etc
for this iam creating labels and Textboxes dynamically added to wrapPanel

Label lblposition=new Label ();
lblposition.Height=25;lblposition.Width=225;lblposition.Content="Position";

// label			   		
Label lblPosX=new Label ();	
lblPosX.Height=25;lblPosX.Width=25;lblPosX.Content="X";
//Textbox
TextBox txtposX=new TextBox();txtposX.Height=25;txtposX.Width=75;txtposX.Text=list1[getindex].Position.X.ToString();
//Button to save
Button btnsaveData=new Button ();
btnsaveData.Height=25;btnsaveData.Width=200;btnsaveData.Content="Save";
btnsaveData.Click+=new RoutedEventHandler(btnsaveData_Click);


//在foreach循环中还会创建一些控件(例如:对于路径,每个单击点都存储在列表中,并且控件要折弯以显示列表中每个点的位置.

属性窗口还包含一个按钮,用于将显示的信息保存在XML文件中.
iam能够创建一个带有控件的窗口,并在从列表中读取的文本框中显示详细信息
问题是


// some controls are also created in foreach loop (eg: for path each click point is stored in list and controls are cretaed to display each point location from list.

property window also contains one button to save the displayed information in a XML file.
iam able to create a window with controls and display the details in the textboxes reading from the list
Problem is

private void btnsaveData_Click(object sender, RoutedEventArgs e)
 {
     // How should i read the textboxes values which are created in the above method.     
 }



iam无法在按钮事件处理程序中读取属性窗口的文本框值

plZ可以有人以正确的方式指导我实现这一目标.

在此先谢谢您.



iam unable to read the textboxes values of the property window in the button eventhandler

plZ can some one guide me in the right way to achieve this.

Thanks in advance.

推荐答案

更改按钮以将ICommand用作其命令,而不是单击.这样,当您创建命令时,可以将相关文本框传递给命令的CommandParameter,WPF框架将自动将其传递给命令的Execute方法.
Change the button to use an ICommand as its command instead of a Click. That way, when you create the command you can pass the relevant textbox to the command''s CommandParameter which will be passed to the Execute method of the command automagically by the WPF framework.


这篇关于在WPF中如何在按钮事件处理程序中读取动态文本框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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