问题
以下是我的代码。我正在使用它在dataGrid中查找textBlocks。
cseg segment 'code'
assume cs:cseg, ds:cseg, ss:cseg, es:cseg
org 100h
start:
mov ah, 09
mov dx, offset Intro
int 21h
mov ah, 01
int 21h
sub al, 30h
mov bl,0Ah
mul bl
mov bl, al
mov ah, 01
int 21h
sub al, 30h
add bl, al
mov cl, bl
jmp again
again:
mov ah, 09
mov dx, offset Hello
int 21h
mov dx, offset msg1
int 21h
mov dx, offset msg
int 21h
inc byte ptr msg
mov al, msg
cmp al, 3ah
jg reset
loopne again
reset:mov byte ptr msg, 30h
inc byte ptr msg1
cmp cx, 0
jg again
jmp done
done:
mov ah, 4ch
int 21h
org 200h
Intro db "Please input how many times you would like the loop to run." , 20h, 20h, "$"
Hello db "hello world" , 20h, 20h, "$"
msg db 30h, 13, 10, "$"
msg1 db 30h, "$"
cseg ends
end start
foreach (TextBlock tb in FindVisualChildren<TextBlock>(dataGrid))
{
// do something
}
然而,问题是我在DataGrid中找不到所有TextBlock。
我怀疑是因为xaml没有完全加载。
由于无法找到的textBlocks是那些在开头没有用xaml写的。它们在运行时由用户添加到dataGrid中。
当前解决方案
我正在使用Dispatcher来确保dataGrid已完全加载,但我认为它太慢了,有时它会产生奇怪的错误。
private static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
}
问题
我想知道是否有其他解决方案可以确保dataGrid完全加载 也许有一些技术可以让dataGrid首先加载?
提前致谢。
XAML
Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() => {
foreach (TextBlock tb in FindVisualChildren<TextBlock>(dataGrid))
{
// do something
}
}