Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为在运行时使用C#XamlReader创建的按钮提供名称并声明事件处理程序_C#_Wpf_Xaml_Xamlparseexception_Xamlreader - Fatal编程技术网

为在运行时使用C#XamlReader创建的按钮提供名称并声明事件处理程序

为在运行时使用C#XamlReader创建的按钮提供名称并声明事件处理程序,c#,wpf,xaml,xamlparseexception,xamlreader,C#,Wpf,Xaml,Xamlparseexception,Xamlreader,我正在使用C#和WPF进行一个项目,该项目动态创建一个包含一些文本、图像和按钮的网格布局。我创建了许多网格,并将它们添加到堆栈面板中。我使用XamlReader.create()函数创建这个网格。这是全部代码,很抱歉Xaml代码太长了 string xaml = "<Grid Height='92' Margin='0,10,0,0' xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>"+

我正在使用C#和WPF进行一个项目,该项目动态创建一个包含一些文本、图像和按钮的网格布局。我创建了许多网格,并将它们添加到堆栈面板中。我使用XamlReader.create()函数创建这个网格。这是全部代码,很抱歉Xaml代码太长了

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);
string xaml=“”+
""+
""+
""+
""+
""+
""+
""+
""+
""+
""+
""+
""+
""+
""+
""+
""+
""+
""+
""+
""+
""+
""+
""+
""+
""+
""+
""+
"";  
//加载面板
StringReader StringReader=新StringReader(xaml);
XmlReader=XmlReader.Create(stringReader);
Grid readerLoadGrid=(Grid)XamlReader.Load(xmlReader);
这对我来说很好,我认为这是在运行时生成网格布局的正确方法,但现在我需要命名按钮并定义事件处理程序,这就不适用了。当我尝试将按钮描述更改为

 <Button x:Name = 'houseButton' Click = 'descriptionButtonClick' ...

您可以在运行时定义网格,就像使用大多数其他控件一样。下面是一个页面,展示了如何:

这样,您就可以使用按钮对象并使用c#访问它们,比如:

按钮myBtn=新按钮();
按钮。单击+=新建…

可以设置按钮的名称属性

//... xaml definition
<Button Name='houseButton' ... />
//... xaml definition

我不能让它工作。每当我试图定义一个名称时,就会出现一个Xaml解析错误。但是FindName方法很有趣。我最终做了这个。我有时花很长时间寻找工作的捷径,以至于我忘记了另一种方法是什么。
var xamlObj = XmlReader.Create(new StringReader(xamlString));
Grid grid = XamlReader.Load(xamlObj);
var btn = (Button)grid.FindName("houseButton");
btn.Click += ...