Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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
Wpf 从其他类访问网格_Wpf_Vb.net - Fatal编程技术网

Wpf 从其他类访问网格

Wpf 从其他类访问网格,wpf,vb.net,Wpf,Vb.net,如何从代码中的不同类访问网格控件 我需要相同的网格实例。 而且似乎不可能共享网格控制 也许我可以通过某种方式对网格的全部内容进行数据绑定 编辑: 好的,这里有一个简化的例子: 我有一个带有网格的简单窗口: <Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/

如何从代码中的不同类访问网格控件

我需要相同的网格实例。 而且似乎不可能共享网格控制

也许我可以通过某种方式对网格的全部内容进行数据绑定

编辑:

好的,这里有一个简化的例子:

我有一个带有网格的简单窗口:

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">

<Grid x:Name="MyGrid">

</Grid>
</Window>
但是,如果我想从window类之外添加新元素,我该怎么做呢?例如:

Class MyNewClass

Sub MySub()
    Dim NewElement As UIElement

    MainWindow.MyGrid.Children.Add(NewElement)
End Sub

End Class

这不起作用,因为它需要一个新实例,但我需要访问相同的原始实例

将数据网格作为参数传递给类。下面的课程是以你的例子为基础的

Class MyNewClass

    Sub MySub(MyGrid as DataGrid)

       Dim NewElement As UIElement
       MyGrid.Children.Add(NewElement)

    End Sub

End Class

创建该类的对象并通过它访问控件?尝试为您的问题提供一些上下文。。。什么网格,什么类?我不知道您想要什么,尽管在WPF中,我们通常不访问UI元素。我建议你要么改进你的问题,要么调查数据绑定。很抱歉含糊不清。为问题添加了更多信息和示例。
Class MyNewClass

    Sub MySub(MyGrid as DataGrid)

       Dim NewElement As UIElement
       MyGrid.Children.Add(NewElement)

    End Sub

End Class