首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从itemsControl设置ItemsPanel的属性

从itemsControl设置ItemsPanel的属性
EN

Stack Overflow用户
提问于 2013-11-13 00:29:47
回答 1查看 569关注 0票数 1

我正在使用我的自定义项目控件(从列表框派生的DisplayPanelControl )的自定义面板,样式类似于下面的XAML

代码语言:javascript
复制
<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有一个属性Edgblending。我想通过我的Items控件来设置这个属性,所以我重写了OnApplyTemplate()方法,并使用VisualTreeHelper来查找设置了所需属性的customPanel。

我想问一下,有没有更好的解决方案来通过Itemscontrol来设置ItemsPanel的属性?

EN

回答 1

Stack Overflow用户

发布于 2013-11-13 20:01:31

这是一种可能的变通方法,它将在2种情况下工作,已经使用了项目控件上的方法OnApplyTemplate()方法。

  1. 当我们在itemsControls模板中通过设置面板
  2. 上的IsItemsHost属性来指定面板时,当我们通过"ItemsPanelTemplate“标记设置项目面板时。

已经通过Ian Griffiths Find Control Inside ListBox? answer给出的解释解决了这个问题。

代码语言:javascript
复制
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类的实现只是在可视化树中查找对象,实现如下(我也从一些博客文章中复制了这一点,但不记得找到链接)

代码语言:javascript
复制
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;
    }

} 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19934588

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档