开发者

Allowing item selection indicator in ListBox to overlay all items in Silverlight

I have a ListBox that uses a WrapPanel for its ItemsPanel, a custom ItemTemplate, and a开发者_StackOverflow 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?


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);
                }
            }
        };
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜