1

我们正在对主屏幕进行更改,并且正在使用 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 放入我们的主窗口?

此致,

古斯塔沃

4

2 回答 2

0

为什么将“this”传递给第二个 FindVisualChildren 调用?(查找VisualChildren(本))。您是否尝试过传递“标签”?:

foreach (RibbonTab tab in FindVisualChildren<RibbonTab>(this.Ribbon))
{ 
    foreach (RibbonButton button in FindVisualChildren<RibbonButton>(tab))
于 2012-05-15T07:59:37.777 回答
0

我有同样的问题,并尝试了2个案例

  1. FindVisualChildren<RibbonButton>(tab)-> 起点选项卡 -> 结果 0
  2. FindVisualChildren<RibbonButton>(window)-> 起点窗口仅从主选项卡获取按钮

我需要来自特定选项卡的按钮,所以我写了以下内容(正在使用选项卡的“项目”属性)......请注意,我只检查“功能区组”中的按钮,所以这是一种特定的解决方案,但你可以明白如何为拿到它,为实现它。

        protected Button GetDeleteButton(Window window, string tabName){
            var tab = FindVisualChildren<RibbonTab>(window).FirstOrDefault(n => n.Name == tabName);
            if (tab == null){
                return null;
            }
            var groups = tab.Items.Cast<object>().Where(n => n is RibbonGroup).Cast<RibbonGroup>();

            foreach (var group in groups){
                var buts = group.Items.Cast<object>().Where(n => n is RibbonButton).Cast<RibbonButton>();
                var myButton = buts.FirstOrDefault(n => (string)n.Tag == "Delete");
                if (myButton != null){
                    return myButton;
                }
            }

            return null;
        }

        public 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;
                    }
                }
            }
        }

希望有所帮助

于 2016-01-22T11:22:41.323 回答