允许列表框中的项目选择指示器覆盖 Silverlight 中的所有项目

发布于 2024-09-13 22:57:19 字数 478 浏览 6 评论 0原文

我有一个 ListBox,它使用 WrapPanel 作为其 ItemsPanel、自定义 ItemTemplate 和自定义 ItemContainerStyle。 ItemContainerStyle 的模板包含一个选择框,当选择某个项目时会显示该选择框。图形设计者希望此选择框与 ListBox 中的同级项目重叠,就像覆盖一样。

我尝试的第一件事是将 ItemContainer 的 Canvas.ZIndex 属性设置为 Selected 状态。这似乎没有效果。然后我读到列表项可能包含在 ContentPresenter 中,因此我创建了一个附加属性来更改项目父级的 ZIndex,但后来我发现 Silverlight 故事板不允许您为自定义动画设置动画附加属性。

有谁知道我们可以使用什么技术来达到我们想要的效果?

I have a ListBox that uses a WrapPanel for its ItemsPanel, a custom ItemTemplate, and a custom ItemContainerStyle. The ItemContainerStyle's template contains a selection box that shows up when an item is selected. The graphics designer would like this selection box to overlap sibling items in the ListBox like it's an overlay.

The first thing I tried was setting the Canvas.ZIndex property of the ItemContainer in the Selected state. That did not seem to have an effect. Then I read that list items might be wrapped inside of a ContentPresenter, so I created an attached property that changes the ZIndex of an item's parent, but then I found out Silverlight storyboards don't let you animate custom attached properties.

Does anyone know of a technique we can use to achieve the effect we desire?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫弦 2024-09-20 22:57:19

我找到了解决方案。基本上,我创建了一个附加属性,该属性在任何选择器(包括列表框)上设置事件处理程序,以便其选择发生更改。当它发生变化时,代码会迭代所有项目容器,根据容器是否代表所选项目来调整 Canvas.ZIndex

public static readonly DependencyProperty SetZIndexOnSelectionProperty = DependencyProperty.RegisterAttached(
    "SetZIndexOnSelection", typeof(bool), typeof(FrameworkUtils), 
    new PropertyMetadata(zIndexSettingChanged));

public static bool GetSetZIndexOnSelection(DependencyObject obj)
{
    return (bool)obj.GetValue(SetZIndexOnSelectionProperty);
}

public static void SetSetZIndexOnSelection(
    DependencyObject obj, bool value)
{
    obj.SetValue(SetZIndexOnSelectionProperty, value);
}

private static void zIndexSettingChanged(
    DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
    if (obj is Selector)
    {
        var selector = obj as Selector;
        selector.SelectionChanged += (s, e) =>
        {
            if (selector.SelectedItem != null)
            {
                foreach (var pair in selector.GetItemsAndContainers())
                {
                    pair.Value.SetValue(
                        Canvas.ZIndexProperty, 
                        (pair.Key == selector.SelectedItem) ? 1 : 0);
                }
            }
        };
    }
}

I found a solution. Basically, I created an attached property that sets up an event handler on any Selector (including ListBox) for when its selection changes. When it changes, the code iterates through all of the item containers, adjusting the Canvas.ZIndex based on whether the container represents the selected item:

public static readonly DependencyProperty SetZIndexOnSelectionProperty = DependencyProperty.RegisterAttached(
    "SetZIndexOnSelection", typeof(bool), typeof(FrameworkUtils), 
    new PropertyMetadata(zIndexSettingChanged));

public static bool GetSetZIndexOnSelection(DependencyObject obj)
{
    return (bool)obj.GetValue(SetZIndexOnSelectionProperty);
}

public static void SetSetZIndexOnSelection(
    DependencyObject obj, bool value)
{
    obj.SetValue(SetZIndexOnSelectionProperty, value);
}

private static void zIndexSettingChanged(
    DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
    if (obj is Selector)
    {
        var selector = obj as Selector;
        selector.SelectionChanged += (s, e) =>
        {
            if (selector.SelectedItem != null)
            {
                foreach (var pair in selector.GetItemsAndContainers())
                {
                    pair.Value.SetValue(
                        Canvas.ZIndexProperty, 
                        (pair.Key == selector.SelectedItem) ? 1 : 0);
                }
            }
        };
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文