[英]How to Getting Buttons inside Tabs of WPF Ribbon not selected
我們正在主屏幕中進行更改,並且正在使用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"/>
因此,這是我們的問題。 我如何將所有RibbonTab的所有RibbonButtons放入我們的主窗口?
最好的祝福,
古斯塔沃
為什么將“ this”傳遞給第二個FindVisualChildren調用? (FindVisualChildren(this))。 您是否嘗試過通過“制表符”?
foreach (RibbonTab tab in FindVisualChildren<RibbonTab>(this.Ribbon))
{
foreach (RibbonButton button in FindVisualChildren<RibbonButton>(tab))
我有同樣的問題,並嘗試了2例
FindVisualChildren<RibbonButton>(tab)
->起點選項卡->結果0 FindVisualChildren<RibbonButton>(window)
->起點窗口僅從主選項卡獲得按鈕 我需要來自特定選項卡的按鈕,因此我寫了以下內容(正在使用Tab的“ Items”屬性)...請注意,我只檢查“ 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;
}
}
}
}
希望有所幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.