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

2 番目の FindVisualChildren 呼び出しに「これ」を渡すのはなぜですか? (FindVisualChildren(this))。代わりに「タブ」を渡そうとしましたか?:

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)-> 開始点ウィンドウはメインタブからのみボタンを取得しました

特定のタブのボタンが必要なので、次のように書きました(タブの「アイテム」プロパティを使用しています)...「RibbonGroups」のボタンのみをチェックしていることに注意してください。それを得るために。

        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 に答える