我们正在对主屏幕进行更改,并且正在使用 WPF。主窗口包含 6 个选项卡,每个选项卡中都有一些按钮。当应用程序启动时,它会验证允许用户打开哪个屏幕(由每个按钮打开)。如果用户无法打开其中一个窗口,则与该屏幕对应的按钮将被禁用,并且图标将发生变化。我们的问题是,我所做的这种方法只将此更改应用于选定的选项卡。
代码:
/// <summary>
/// Encontra todos os objetos na tela
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="objetoTela"></param>
/// <returns></returns>
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject objetoTela) where T : DependencyObject
{
if (objetoTela != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(objetoTela); i++)
{
DependencyObject objetoFilho = VisualTreeHelper.GetChild(objetoTela, i);
if (objetoFilho != null && objetoFilho is T)
{
yield return (T)objetoFilho;
}
foreach (T componenteObjetoFilho in FindVisualChildren<T>(objetoFilho))
{
yield return componenteObjetoFilho;
}
}
}
}
这个方法 FindVisualChildren() 在我们的窗口中搜索指定的类型。你可以在这里看到:
foreach (RibbonTab tab in FindVisualChildren<RibbonTab>(this.Ribbon))
代码:
foreach (MenuResources item in Enum.GetValues(typeof(MenuResources)))
{
foreach (RibbonTab tab in FindVisualChildren<RibbonTab>(this.Ribbon))
{
foreach (RibbonButton button in FindVisualChildren<RibbonButton>(tab))
{
if (button.Name.Equals("rbb" + item.ToString()))
{
if (authorizationService.CheckAccess((Int64)item, true))
{
button.LargeImageSource = (ImageSource)FindResource("ImageMenu" + item.ToString());
button.IsEnabled = true;
}
else
{
button.LargeImageSource = (ImageSource)FindResource("ImageMenuDesabilitado" + item.ToString());
button.IsEnabled = false;
}
}
}
}
}
这是我们的 RibbonTabs(例如,这里没有按钮,只有选项卡):
<ribbon:RibbonTab x:Name="rbtOperacaoLCG" ContextualTabGroupHeader="Operação" Header="LCG" BorderBrush="White" KeyTip="O" Foreground="Black" Background="White"/>
<ribbon:RibbonTab x:Name="rbtSeguranca" Header="Segurança" KeyTip="S" Foreground="Black" FontWeight="Normal"/>
<ribbon:RibbonTab x:Name="rbtManutencao" Header="Manutenção" KeyTip="M" Foreground="Black"/>
<ribbon:RibbonTab x:Name="rbtComunicacao" Header="Comunicação" KeyTip="C" Foreground="Black" />
<ribbon:RibbonTab x:Name="rbtOperacaoComum" ContextualTabGroupHeader="Operação" Header="Comum" BorderBrush="White" KeyTip="O" Foreground="Black" Background="White" IsSelected="True"/>
<ribbon:RibbonTab x:Name="rbtOperacaoLTQ" ContextualTabGroupHeader="Operação" Header="LTQ" BorderBrush="White" KeyTip="O" Foreground="Black" Background="White"/>
所以,这是我们的问题。我怎样才能将所有 RibbonTabs 的所有 RibbonButtons 放入我们的主窗口?
此致,
古斯塔沃