命名并声明在运行时使用 C# XamlReader 创建的按钮的事件处理程序 [英] Give a name and declare event handler of a button Created at run time with C# XamlReader
问题描述
我正在使用 C# 和 WPF 开发一个项目,该项目动态创建包含一些文本、图像和按钮的网格布局.我创建了许多这样的网格,并将它们添加到堆栈面板中.我使用 XamlReader.Create() 函数创建了这个网格.这是整个代码,我很抱歉 Xaml 代码太长
I am working on a project in C# and WPF that dynamically creates a grid layout that has some text, an image, and a button. I create many of these grids, and add them to a stack panel. I create this grid using the XamlReader.Create() function. Here is the whole code, and i apologize that the Xaml code is so long
string xaml = "<Grid Height='92' Margin='0,10,0,0' xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>"+
"<Grid.Background>"+
"<LinearGradientBrush EndPoint='0.5,1' StartPoint='0.5,0'>"+
"<GradientStop Color='#FFACD7FF' Offset='0.172'/>"+
"<GradientStop Color='#FFACD7FF' Offset='0.508'/>"+
"<GradientStop Color='#FF69B5FF' Offset='0.996'/>"+
"<GradientStop Color='#FF79BEFB' Offset='0.017'/>"+
"<GradientStop Color='#FFACD7FF' Offset='0.877'/>"+
"</LinearGradientBrush>"+
"</Grid.Background>"+
"<Grid.RowDefinitions>"+
"<RowDefinition Height='0.522*'/>"+
"<RowDefinition Height='0.478*'/>"+
"</Grid.RowDefinitions>"+
"<Grid.ColumnDefinitions>"+
"<ColumnDefinition Width='133'/>"+
"<ColumnDefinition Width='73.5'/>"+
"<ColumnDefinition Width='0.833*'/>"+
"<ColumnDefinition Width='0.167*'/>"+
"<ColumnDefinition Width='108.775'/>"+
"</Grid.ColumnDefinitions>"+
"<Image Source='stockHome.jpg' Margin='8' Grid.Column='4' Grid.RowSpan='2'/>"+
"<TextBlock TextWrapping='Wrap' Text='Details: "+
addHouse.description+
"' Margin='8' Grid.Column='2' HorizontalAlignment='Center'/>"+
"<Button Content='View Full Description' Grid.Column='2' HorizontalAlignment='Center' Margin='4,8,0,8' Grid.Row='1' Width='134.667'/>"+
"<Label Content='$"+
addHouse.cost+
"' Margin='8,8,0,0' FontSize='21.333' FontFamily='Segoe UI Semibold' HorizontalAlignment='Left' RenderTransformOrigin='0.53,1.474'/>"+
"<Label Content='"+
addHouse.neighborhood+
"' Margin='8,0,0,8' Grid.Row='1' HorizontalAlignment='Left' Width='117' FontSize='18.667'/>"+
"<TextBlock Grid.Column='1' Margin='8,15,8,8' Grid.RowSpan='2' TextWrapping='Wrap'><Run Text='"+
addHouse.type+
"'/><LineBreak/><Run Text='"+
addHouse.bedrooms+
" Bed'/><LineBreak/><Run Text='"+
addHouse.bathrooms+
" Bath'/><LineBreak/><Run Text='ID: "+
addHouse.ID+
"'/></TextBlock>"+
"<Image Grid.Column='3' Margin='8,23.667,8,20.251' Grid.RowSpan='2' Source='HeartPop.png'/>"+
"</Grid>";
// Load the panel
StringReader stringReader = new StringReader(xaml);
XmlReader xmlReader = XmlReader.Create(stringReader);
Grid readerLoadGrid = (Grid)XamlReader.Load(xmlReader);
这对我来说效果很好,我认为这将是在运行时生成网格布局的正确方法,但是现在我需要命名按钮并定义一个事件处理程序,这会崩溃.当我尝试将按钮描述更改为
This has worked fine for me, and i thought this would be the correct way to generate a grid layout at runtime, but now that i need to name the button, and define an event handler, this falls apart. when i try to change the button description to
<Button x:Name = 'houseButton' Click = 'descriptionButtonClick' ...
我收到 XAMLParse 异常.读完之后,我不太确定您是否可以在运行时在 xaml 中声明这些东西.所以现在,我对如何做到这一点感到非常困惑.我不能在创建网格后更改名称,因为它没有名称可供调用.有没有办法访问 UIElement 的子元素,即使它没有名字?或者有没有办法在运行时在 XAML 中声明名称而我只是做错了?
I get a XAMLParse exception. After reading about this, i am not so sure that you can declare these things in xaml at runtime. So now, I am very confused on how else to do this. I cant just change the name after i have created the grid, as it has no name to be called by. is there a way to access a child of a UIElement even if it has no name? or is there a way to declare the name in XAML at runtime and im just doing it wrong?
推荐答案
您可以像使用大多数其他控件一样在运行时定义网格.这是一个显示示例的页面:
You can define the Grid on runtime like you can do it with most other controls. here is a page showing an example how:
http://sullivan.net/blog/2009/09/creating-a-wpf-grid-programmatically-code-behind/
您将能够使用按钮对象并使用 c# 访问它们,例如:
than you'll be able to use button objects and access them using c# like:
按钮 myBtn = new Button();Button.Click += new ....
Button myBtn = new Button(); Button.Click += new ....
这篇关于命名并声明在运行时使用 C# XamlReader 创建的按钮的事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!