silverlight - 将 ItemsControl.Items 枚举为 UIElements

标签 silverlight itemscontrol uielement

我有一个通过 ItemsControl 显示的超链接列表,如下所示:

 <ItemsControl x:Name="SubMenu" Visibility="Collapsed">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <HyperlinkButton Content="{Binding Name}"
                                 NavigateUri="{Binding Url}"
                                 TargetName="ContentFrame"
                                 Style="{StaticResource LinkStyle}"
                                 />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Style="{StaticResource LinksStackPanelStyle}"
                            VerticalAlignment="Center"
                            HorizontalAlignment="Left" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>

我需要做的是枚举子菜单中的实际超链接,如下所示:
    foreach (UIElement child in SubMenu.Items) // this does not work!
    {
        HyperlinkButton hb = child as HyperlinkButton;
        if (hb != null && hb.NavigateUri != null)
        {
            if (hb.NavigateUri.ToString().Equals(e.Uri.ToString()))
            {
                VisualStateManager.GoToState(hb, "ActiveLink", true);
            }
            else
            {
                VisualStateManager.GoToState(hb, "InactiveLink", true);
            }
        }
    }

问题是我似乎找不到在 ItemsCollection.Items 中枚举实际 UI 元素的方法。

任何人都知道如何做到这一点或可能的解决方法?

我可以提到我正在尝试做的是构建一个菜单和子菜单,将点击的超链接显示为一种面包屑。

更新:
最好的事情是,如果我能以某种方式进入该堆栈面板,因为此代码似乎有效:
    foreach (UIElement child in LinksStackPanel.Children)
    {
        HyperlinkButton hb = child as HyperlinkButton;
        if (hb != null && hb.NavigateUri != null)
        {
            if (hb.NavigateUri.ToString().Equals(e.Uri.ToString()))
            {
                VisualStateManager.GoToState(hb, "ActiveLink", true);
            }
            else
            {
                VisualStateManager.GoToState(hb, "InactiveLink", true);
            }
        }
    }

最佳答案

解决方案如下所示:

foreach (var item in SubMenu.Items)
{
    var hb = SubMenu.ItemContainerGenerator.ContainerFromItem(item).FindVisualChild<HyperlinkButton>();

    if (hb.NavigateUri.ToString().Equals(e.Uri.ToString()))
    {
        VisualStateManager.GoToState(hb, "ActiveLink", true);
    }
    else
    {
        VisualStateManager.GoToState(hb, "InactiveLink", true);
    }
}

扩展方法 FindVisualChild:
public static T FindVisualChild<T>(this DependencyObject instance) where T : DependencyObject
{
    T control = default(T);

    if (instance != null)
    {

        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(instance); i++)
        {
            if ((control = VisualTreeHelper.GetChild(instance, i) as T) != null)
            {
                break;
            }

            control = FindVisualChild<T>(VisualTreeHelper.GetChild(instance, i));
        }
    }

    return control;

}

关于silverlight - 将 ItemsControl.Items 枚举为 UIElements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2336989/

相关文章:

ios - 从 iOS 中的自定义 UI 元素获取父类名称

c# - 如何在 Silverlight 4 中等待状态改变转换完成?

silverlight - 以串行方式在 Silverlight 中运行异步操作

c# - WPF ItemsControl 和 WrapPanel 走进酒吧

c# - 使用mvvm动态创建控件

c# - 从动画完成事件中获取 UIElement

wpf - 如何让 UIElement 支持绑定(bind)?

silverlight - 为什么当我将绑定(bind)添加到 TextBlock 时 Visual Studio 2010 会崩溃?

silverlight - 如何在 silverlight 应用程序中播放 youtube 视频?

c# - WPF:聚合 ListBox 上的属性