C# 如果按钮是网格的子项,如何更改其内容?
我想在按钮2的点击事件中更改按钮1的内容。但无法获取列表UiList中网格的子按钮类的对象。 请指导我找到正确的方法来查看和解决它。并指导如果对象是在运行时生成的,那么如何访问它C# 如果按钮是网格的子项,如何更改其内容?,c#,silverlight,windows-phone-7,C#,Silverlight,Windows Phone 7,我想在按钮2的点击事件中更改按钮1的内容。但无法获取列表UiList中网格的子按钮类的对象。 请指导我找到正确的方法来查看和解决它。并指导如果对象是在运行时生成的,那么如何访问它 public部分类主页:PhoneApplicationPage { List UIList=新列表(); Grid objGrid1=null; 按钮objButton1=null; 按钮objButton2=null; //建造师 公共主页() { 初始化组件(); createGrid1(“grid1”);
public部分类主页:PhoneApplicationPage
{
List UIList=新列表();
Grid objGrid1=null;
按钮objButton1=null;
按钮objButton2=null;
//建造师
公共主页()
{
初始化组件();
createGrid1(“grid1”);
创建按钮2(“按钮2”);
}
公共void createGrid1(字符串x)
{
objGrid1=新网格();
objGrid1.高度=100;
objGrid1.Name=x;
对象1.宽度=200;
objGrid1.边距=新厚度(100,100,0,0);
objGrid1.HorizontalAlignment=System.Windows.HorizontalAlignment.Left;
objGrid1.VerticalAlignment=System.Windows.VerticalAlignment.Top;
objGrid1.Background=新的SolidColorBrush(Colors.Orange);
createButton1(“变更名称”);
}
public void createButton1(字符串_名称)
{
objButton1=新按钮();
objButton1.高度=90;
objButton1.Name=_Name;
objButton1.Content=“Button1”;
objButton1.FontSize=20;
objButton1.宽度=190;
objButton1.HorizontalAlignment=System.Windows.HorizontalAlignment.Left;
objButton1.VerticalAlignment=System.Windows.VerticalAlignment.Top;
objButton1.Background=新的SolidColorBrush(Colors.Blue);
objButton1.Foreground=新的SolidColorBrush(Colors.White);
objGrid1.Children.Add(objButton1);
LayoutRoot.Children.Add(objGrid1);
UIList.Add(objGrid1);
}
public void createButton2(字符串_名称)
{
objButton2=新按钮();
objButton2.边距=新厚度(240,300,0,0);
objButton2.Name=_Name;
objButton2.高度=90;
objButton2.Content=“Button2”;
objButton2.FontSize=20;
objButton2.宽度=190;
objButton2.HorizontalAlignment=System.Windows.HorizontalAlignment.Left;
objButton2.VerticalAlignment=System.Windows.VerticalAlignment.Top;
objButton2.Background=新的SolidColorBrush(Colors.Blue);
objButton2.Foreground=新的SolidColorBrush(Colors.White);
LayoutRoot.Children.Add(objButton2);
objButton2.单击+=(s,e)=>
{
int c=UIList.ElementAt(0.Children.Count);
如果(c==1)
{
//单击按钮2时更改按钮1内容的逻辑
}
};
}
}
假设您不能只保留对已创建控件的引用,例如在类字段中,您可以遍历的属性并找到所需的按钮。如果有多个按钮,可以使用属性来区分它们
找到按钮后,使用该属性更改按钮的内容,如上面内容中所述。Does
objButton1.Content=“SomthingElse”
不起作用?它确实起作用,但如果您的对象是在运行时生成的,那么如何访问该对象。然后在类中创建一个字段,并在创建时将按钮存储在该字段中?但在运行时创建的网格本身不会更新。
public partial class MainPage : PhoneApplicationPage
{
List<Grid> UIList = new List<Grid>();
Grid objGrid1 = null;
Button objButton1 = null;
Button objButton2 = null;
// Constructor
public MainPage()
{
InitializeComponent();
createGrid1("grid1");
createButton2("Button2");
}
public void createGrid1(string x)
{
objGrid1 = new Grid();
objGrid1.Height = 100;
objGrid1.Name = x;
objGrid1.Width = 200;
objGrid1.Margin = new Thickness(100, 100, 0, 0);
objGrid1.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
objGrid1.VerticalAlignment = System.Windows.VerticalAlignment.Top;
objGrid1.Background = new SolidColorBrush(Colors.Orange);
createButton1("changename");
}
public void createButton1(string _name)
{
objButton1 = new Button();
objButton1.Height = 90;
objButton1.Name = _name;
objButton1.Content="Button1";
objButton1.FontSize = 20;
objButton1.Width = 190;
objButton1.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
objButton1.VerticalAlignment = System.Windows.VerticalAlignment.Top;
objButton1.Background = new SolidColorBrush(Colors.Blue);
objButton1.Foreground = new SolidColorBrush(Colors.White);
objGrid1.Children.Add(objButton1);
LayoutRoot.Children.Add(objGrid1);
UIList.Add(objGrid1);
}
public void createButton2(string _name)
{
objButton2 = new Button();
objButton2.Margin = new Thickness(240, 300, 0, 0);
objButton2.Name = _name;
objButton2.Height = 90;
objButton2.Content = "Button2";
objButton2.FontSize = 20;
objButton2.Width = 190;
objButton2.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
objButton2.VerticalAlignment = System.Windows.VerticalAlignment.Top;
objButton2.Background = new SolidColorBrush(Colors.Blue);
objButton2.Foreground = new SolidColorBrush(Colors.White);
LayoutRoot.Children.Add(objButton2);
objButton2.Click += (s, e) =>
{
int c = UIList.ElementAt(0).Children.Count;
if (c == 1)
{
//logic to change content of Button1 on click of Button2
}
};
}
}