简体   繁体   中英

Setting ItemsPanel's properties from itemsControl

I am using custom panel from my custom Items control( DisplayPanelControl which is derived from List box) the style is some thing similar to following XAML

<Style x:Key="ContainerStyle" TargetType="{x:Type local:DisplayPanelControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Grid>                   
                    <local:CustomePanel Background="AliceBlue" IsItemsHost="True">
                    </local:CustomePanel>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

the CustomePanel has a property Edgblending. I want to set this property through my Items control, so I have overridden OnApplyTemplate() method and used VisualTreeHelper to find my customPanel as set the desired property.

I want to ask if there is a better solution for setting properties on ItemsPanel through Itemscontrol?

This is one of the possible work around which will work in under 2 situation, Have used the method on Items controls OnApplyTemplate() method.

  1. When we specify Panel in itemsControls template by setting IsItemsHost property on the Panel
  2. When we set the Items panel via "ItemsPanelTemplate" markup.

Have come to this workaround by the explanation given by Ian Griffiths Find Control Inside ListBox? answer.

private T GetItemsPanel<T>(ItemsControl itemsControl) where T : Panel
    {
        T _Panel = UIHelper.FindVisualChild<T>(itemsControl);
        if (_Panel == null)
        {
            ItemsPresenter itemsPresenter = UIHelper.FindVisualChild<ItemsPresenter>(itemsControl);
            if (itemsPresenter != null)
            {
                itemsPresenter.ApplyTemplate();
                _Panel = VisualTreeHelper.GetChild(itemsPresenter, 0) as T;
            }
        }
        return _Panel;
    }  

The implementation for UiHelper class is nothing but finding the object in visual tree and implementation is as below(I have copied this as well from some blog post but cant remember to find the link)

public static class UIHelper
{
    /// <summary>
    /// Finds a parent of a given item on the visual tree.
    /// </summary>
    /// <typeparam name="T">The type of the queried item.</typeparam>
    /// <param name="child">A direct or indirect child of the queried item.</param>
    /// <returns>The first parent item that matches the submitted type parameter. 
    /// If not matching item can be found, a null reference is being returned.</returns>
    public static T FindVisualParent<T>(DependencyObject child)
      where T : DependencyObject
    {
        // get parent item
        DependencyObject parentObject = VisualTreeHelper.GetParent(child);

        // we’ve reached the end of the tree
        if (parentObject == null) return null;

        // check if the parent matches the type we’re looking for
        T parent = parentObject as T;
        if (parent != null)
        {
            return parent;
        }
        else
        {
            // use recursion to proceed with next level
            return FindVisualParent<T>(parentObject);
        }
    }
    public static T FindVisualChild<T>(DependencyObject parent) where T : DependencyObject
    {
        T child = default(T);

        int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < numVisuals; i++)
        {
            Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
            child = v as T;
            if (child == null)
            {
                child = FindVisualChild<T>(v);
            }
            if (child != null)
            {
                break;
            }
        }
        return child;
    }

} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM