我有querys絃樂數據庫的功能(這是X:NAME =「」在我的應用程序)參考的元素一個網格內的WPF
以下是代碼:
Try
For Each s In output
Dim nameOfControl = s
Dim window = Windows.Application.Current.Windows(0)
Dim visuals = GetVisualChildren(Of FrameworkElement)(window)
Dim child = visuals.OfType(Of FrameworkElement)().FirstOrDefault(Function(x) x.Name = nameOfControl)
child.Visibility = Visibility.Collapsed
Next
Public Iterator Function GetVisualChildren(Of T As Visual)(parent As DependencyObject) As IEnumerable(Of T)
Dim child As T = Nothing
Dim numVisuals As Integer = VisualTreeHelper.GetChildrenCount(parent)
For i As Integer = 0 To numVisuals - 1
Dim v As Visual = DirectCast(VisualTreeHelper.GetChild(parent, i), Visual)
child = TryCast(v, T)
If v IsNot Nothing Then
For Each item In GetVisualChildren(Of T)(v)
Yield item
Next
End If
If child IsNot Nothing Then
Yield child
End If
Next
End Function
我似乎無法弄清楚爲什麼這不適用於所有元素,即。我的窗口由TabControl
組成,我可以通過「x名稱」呼叫,toolBar
也是如此,即使使用TabItem's
,一切仍然正常,但我不能參考網格中的<Button x:Name="buttonRefresh"
。
我收到上線波紋管一個錯誤的NullReferenceException:
child.Visibility = Visibility.Collapsed
如果我插入在該行斷點我得到這個{System.Windows.Controls.TabItem Header:FirstTab Content:}
爲child
所以我假設我需要改變這段代碼
Dim window = Windows.Application.Current.Windows(0)
那麼,在這種情況下我能做些什麼嗎? – Thomas
查看我的更新。祝你好運。 –
對不起,該匹配返回所有標籤'{System.Windows.Controls。TabItem標題:標籤二內容:} ** **標籤二或標籤三**。但是對於nameOfControl「refreshButton」,我在** match **中沒有得到任何東西 – Thomas