C# 如何从VB/C代码访问菜单中的文本框
我需要从代码访问文本框等方面的帮助。我使用VB,但也欢迎使用C#示例。 如果我试图通过键入txtSettingsCity来访问文本框,VB将无法识别该文本框 我非常感谢你的帮助。先谢谢你C# 如何从VB/C代码访问菜单中的文本框,c#,vb.net,xaml,C#,Vb.net,Xaml,我需要从代码访问文本框等方面的帮助。我使用VB,但也欢迎使用C#示例。 如果我试图通过键入txtSettingsCity来访问文本框,VB将无法识别该文本框 我非常感谢你的帮助。先谢谢你 <Button x:Name="btnSettings" FontSize="16" FontFamily="Segoe MDL2 Assets" Content="" Foreground="{StaticResource NuhmeTextColor}"
<Button x:Name="btnSettings" FontSize="16" FontFamily="Segoe MDL2 Assets" Content="" Foreground="{StaticResource NuhmeTextColor}" RelativePanel.AlignLeftWithPanel="True" RelativePanel.AlignBottomWithPanel="True" Margin="4,0,0,4">
<Button.Flyout>
<MenuFlyout x:Name="mfSettings">
<MenuFlyoutItem x:Name="mfiSettings">
<MenuFlyoutItem.Template>
<ControlTemplate TargetType="MenuFlyoutItem">
<StackPanel Margin="4,10,4,0" Orientation="Vertical" Width="130">
<TextBox x:Name="txtSettingsCity" Header="Din placering" PlaceholderText="Ribe, Danmark" Width="130" Margin="0,0,0,10" ToolTipService.ToolTip="Indtast din placering eller klik søg"/>
<Button x:Name="btnFindLocation" Content="Søg" Margin="80,0,0,10" Click="btnFindLocation_Click"/>
</StackPanel>
</ControlTemplate>
</MenuFlyoutItem.Template>
</MenuFlyoutItem>
</MenuFlyout>
</Button.Flyout>
</Button>
您可以通过VisualTreeHelper.GetParent循环,首先获取父菜单UfLyoutItem,然后递归使用VisualTreeHelper.GetChild返回文本框 但是,我强烈建议探索MVVM方法,将Button.Command绑定到ICommand,并将TextBox绑定作为Button.CommandParameter传递 快速肮脏的例子
private void btnFindLocation_OnClick(object sender, RoutedEventArgs e)
{
var target = sender as DependencyObject;
while (target != null)
{
target = VisualTreeHelper.GetParent(target);
if (target is MenuFlyoutItem menuFlyoutItem)
{
if (GetChild<TextBox>(menuFlyoutItem) is TextBox textBox)
{
// Access the TextBox here
Debug.WriteLine(textBox.Text);
}
}
}
}
private TFrameworkElement GetChild<TFrameworkElement>(FrameworkElement parent) where TFrameworkElement : FrameworkElement
{
var count = VisualTreeHelper.GetChildrenCount(parent);
for (var index = 0; index < count; ++index)
{
var child = VisualTreeHelper.GetChild(parent, index) as FrameworkElement;
if (child is TFrameworkElement frameworkElement)
{
return frameworkElement;
}
else
{
if (GetChild<TFrameworkElement>(child) is TFrameworkElement grandChild)
{
return grandChild;
}
}
}
return null;
}
private void btnFindLocation\u OnClick(对象发送方,RoutedEventArgs e)
{
var target=发送方作为DependencyObject;
while(目标!=null)
{
target=VisualTreeHelper.GetParent(目标);
if(目标为MenuFlyoutItem MenuFlyoutItem)
{
如果(GetChild(menuFlyoutItem)为文本框文本框)
{
//访问此处的文本框
Debug.WriteLine(textBox.Text);
}
}
}
}
私有TFrameworkElement GetChild(FrameworkElement父级),其中TFrameworkElement:FrameworkElement
{
变量计数=VisualTreeHelper.GetChildrenCount(父级);
对于(var指数=0;指数<计数;++指数)
{
var child=VisualTreeHelper.GetChild(父级,索引)作为FrameworkElement;
if(子项为TFrameworkElement frameworkElement)
{
返回框架元素;
}
其他的
{
if(GetChild(child)是TFrameworkElement孙子)
{
还孙子;
}
}
}
返回null;
}
为什么不使用VisualTreeHelper
,这里是一个快速代码示例
Public Shared Function FindParent(Of T As DependencyObject)(ByVal child As DependencyObject) As T
Dim parentObject As DependencyObject = VisualTreeHelper.GetParent(child)
If parentObject Is Nothing Then Return Nothing
Dim parent As T = TryCast(parentObject, T)
If parent IsNot Nothing Then Return parent Else Return FindParent(Of T)(parentObject)
End Function
函数的用法:
Dim parent As ControlTypeHere= FindParent(Of ControlTypeHere)(Me)
这不是一个答案,而是几句建议。。。。请在评论中添加这些建议,非常感谢您的解决方案。然而,我不能让它工作。我收到一个错误“NullReferenceException”,哪一行引发异常?我使用文本框在这里键入ControlType。我在一个类和一个模块中尝试了你的函数。同样的结果。抛出异常的不是您的函数,而是我读取结果的方法。Debug.WriteLine(parent.Name)抛出一个异常No…..Lolz…这段代码是为了找到子控件的父控件…比如,你有一个页面,你在一个框架中显示它…然后你可以使用我的方法找到页面的父控件,这是一个框架…无论如何,澄清你的情况,我会更新我的答案谢谢你