我正在使用WPF MVVM模式。我有一个带有多个选项卡的TabControl所有这些选项卡都使用相同的viewmodel。所有验证都正常工作,你可以在附件中看到我在侧面列表视图中显示所有验证错误。这是我的xaml代码:
<TabControl Grid.Row="2" Grid.Column="0" VerticalAlignment="Top" SelectedIndex="{Binding SelectedTabIndex}"
DisplayMemberPath="Content" DropDownDisplayMode="Visible" ScrollMode="Item" BorderThickness="0">
<TabItem Header="Job Config" DropDownContent="Job Config" >
<TabItem.Content>
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<local:JobGroupsCars DataContext="{Binding}" Margin="10" IsEnabled="{Binding Job.IsNotInEditMode ,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
</ScrollViewer>
</TabItem.Content>
</TabItem>
<TabItem DropDownContent="Job Info" Header="Job Info">
<TabItem.Content>
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<local:JobDetailView DataContext="{Binding}" Margin="10" IsEnabled="{Binding Job.IsNotInEditMode ,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
</ScrollViewer>
</TabItem.Content>
</TabItem>
<TabItem DropDownContent="Shipping" Header="Shipping Details">
<TabItem.Content>
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<local:JobShippingView DataContext="{Binding}" Margin="10" IsEnabled="{Binding Job.IsNotInEditMode ,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
</ScrollViewer>
</TabItem.Content>
</TabItem>
<TabItem DropDownContent="Controller" Header="Controller">
<TabItem.Content>
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<local:ControllerView DataContext="{Binding}" Margin="10" IsEnabled="{Binding Job.IsNotInEditMode ,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
</ScrollViewer>
</TabItem.Content>
</TabItem>
<TabItem DropDownContent="Ac Motor and Brakes" Header="AC Motor and Brakes" >
<TabItem.Content>
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<local:ACMotorView DataContext="{Binding}" Margin="10" IsEnabled="{Binding Job.IsNotInEditMode ,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
</ScrollViewer>
</TabItem.Content>
</TabItem>
</TabControl>
我的任务是当我点击右侧面板中的按钮时,它必须导航到带有此验证错误的选项卡。
请任何帮助将不胜感激。
我不知道有什么方法可以实现这一点,但从你一直在寻找解决方案的事实来判断,我为你做了一些可怕的事情。首先添加这个类
public static class UIHelper
{
public static T FindChild<T>(this DependencyObject parent, string childName)
where T : DependencyObject
{
if (parent == null) return null;
T foundChild = null;
var childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (var i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
var childType = child as T;
if (childType == null)
{
foundChild = FindChild<T>(child, childName);
if (foundChild != null) break;
}
else if (!string.IsNullOrEmpty(childName))
{
var frameworkElement = child as FrameworkElement;
if (frameworkElement != null && frameworkElement.Name == childName)
{
foundChild = (T)child;
break;
}
}
else
{
foundChild = (T)child;
break;
}
}
return foundChild;
}
}
这堂课将根据他们的名字找到孩子。我也把它们放在一起来代表你的情况:
<TabControl Name="ImTheTabControl">
<TabItem Name="ImTheTab">
<Grid>
<TextBox Name="ImTheTextbox"></TextBox>
</Grid>
</TabItem>
<TabItem Name="ImNotTheTab"/>
<TabItem Name="ImNotTheTabEighter"/>
</TabControl>
现在在您的代码中使用包含错误的控件调用此函数:
void findPapaTab(Control ctrl)
{
foreach (TabItem item in ImTheTabControl.Items)
{
if (null != UIHelper.FindChild<TextBox>(item.Content as Grid, ImTheTextbox.Name))
{
MessageBox.Show(item.Name);
}
}
}