代码之家  ›  专栏  ›  技术社区  ›  Jacob

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

  •  0
  • Jacob  · 技术社区  · 14 年前

    我有一个 ListBox 使用一个 WrapPanel 因为它 ItemsPanel ,一种习俗 ItemTemplate ,一种习俗 ItemContainerStyle . ItemContainerStyle的模板包含一个选择框,当选中一个项目时,该框会显示出来。图形设计器希望此选择框与列表框中的同级项重叠,就像它是一个覆盖。

    Canvas.ZIndex ContentPresenter ,所以我创建了一个附加属性来更改项目父级的ZIndex,但是后来我发现Silverlight故事板不允许您设置自定义附加属性的动画。

    有人知道我们可以用什么方法来达到我们想要的效果吗?

    1 回复  |  直到 14 年前
        1
  •  1
  •   Jacob    14 年前

    我找到了解决办法。基本上,我创建了一个附加属性,在任何 Selector (包括 ListBox 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);
                    }
                }
            };
        }
    }