Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.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# 从其他类访问控件_C#_Wpf_Xaml - Fatal编程技术网

C# 从其他类访问控件

C# 从其他类访问控件,c#,wpf,xaml,C#,Wpf,Xaml,我需要从不同的类访问XAML中的控件,例如,名为myTextBox的文本框。这就是我所做的,但出现了一个错误: Windows1 w = new Windows1(); w.myTextBox.text = "Hello!"; 但是,我收到的错误消息如下: 'Set property'System.Windows.Controls.ContentControl.Content'抛出 例外情况。 粘性错误指向 <Grid Margin="10"> <Grid

我需要从不同的类访问XAML中的控件,例如,名为myTextBox的文本框。这就是我所做的,但出现了一个错误:

   Windows1 w = new Windows1();
   w.myTextBox.text = "Hello!";
但是,我收到的错误消息如下:

'Set property'System.Windows.Controls.ContentControl.Content'抛出 例外情况。

粘性错误指向

<Grid Margin="10">
    <Grid.RowDefinitions> <!-- error points to this line -->
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

请给我一些指导,找出原因或错误

谢谢,
Amit

还有其他一些事情正在发生,因为我能够在一个简单的应用程序中实现这一点,而不需要做任何棘手的事情。那么,您的场景中还有什么?您的“不同”类是否在后台线程中运行?如果是这样,您可能需要使用dispatcher来访问UI线程。

请检查以下内容:

它将在可视化树中找到给定项的子项

用法:

var recentCommitMessages = UIHelper.FindChild<ComboBox>(this, "RecentCommitMessages");
var recentCommitMessages=UIHelper.FindChild(这是“recentCommitMessages”);

在这里,它将搜索所有ComboBox元素,以查找id为“RecentCommitMessages”的元素。

我正试图通过一个名为OnDrop()的方法访问另一个类中的控件,因此当在画布上拖放对象时,我必须将其id存储在列表中,并在文本框中显示它,它说:无法将lambda表达式转换为“System.Delegate”类型,因为它不是委托类型使用
Dispatcher.Invoke()
两种情况下仍然会出现错误确实感谢您的评论,但在运行之前,VS抱怨如下:System.Action不包含带0个参数的构造函数。噢,我目前无法访问Visual Studio。对不起。您正在使用哪个版本的.NET Framework?您还可以使用:
w.Dispatcher.Invoke((操作)委托(){w.myTextBox.text=“Hello!”;})
w.Dispatcher.Invoke((操作)({w.myTextBox.text=“Hello!”;}))UIHelper无法识别。有什么想法吗?好的,我包含了这个类,它总是返回null.sure`if(content!=null){newObject=new-DroppedObject();newObject.content=content;//获取现在的标记类型typeofDroppedOjbect=content.GetType();Object o=t.GetProperty(“Tag”).GetValue(content,null);newItem.Tag=(string)o;var q=UIHelper.FindChild(此为“textSearchElement”);`是否将id“textSearchElement”添加到textbox元素?必须将搜索元素命名为
x:name=“textSearchElement
,而
FindChild
的第一个参数是查找它的范围。您传递了
,您确定这是您想要的吗?
是用户控件还是什么?请尝试
UIHelper.FindChild(Application.Current.MainWindow,“textSearchElement”)
。请给我们内部异常。
var recentCommitMessages = UIHelper.FindChild<ComboBox>(this, "RecentCommitMessages");