从项目设置项目面板的属性控件



我正在使用自定义面板从我的自定义项目控件( 显示面板控件派生自列表框)样式类似于遵循 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>

CustomePanel 具有一个属性 Edgegblending。我想通过我的 Items 控件设置此属性,所以我重写了 OnApplyTemplate() 方法并使用 VisualTreeHelper 查找我的自定义面板作为设置所需的属性。

我想问是否有更好的解决方案通过项目控件在项目面板上设置属性?

这是在 2 种情况下可行的解决方法之一,在项目控件上使用过该方法 OnApplyTemplate() 方法。

  1. 当我们通过在面板上设置 IsItemsHost 属性在项目控件模板中指定面板时
  2. 当我们通过"项目面板模板"标记设置项目面板时。

通过 Ian Griffiths 在 ListBox 内查找控件给出的解释得出了此解决方法? 答案。

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

UiHelper 类的实现只不过是在可视化树中查找对象,实现如下(我也从一些博客文章中复制了这个,但不记得找到链接)

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

最新更新