如何在代码中获取ListBox ItemsPanel

时间:2011-09-06 12:30:56

标签: c# .net wpf code-behind itemspanel

我有一个带有ItemsPanel的ListBox

<Setter Property="ItemsPanel">
    <Setter.Value>
        <ItemsPanelTemplate>
             <StackPanel x:Name="ThumbListStack" Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </Setter.Value>
</Setter>

我想在后面的代码中使用TranslateTransform沿X轴移动堆栈面板。

问题是,我找不到Stack Panel。

ThumbListBox.FindName("ThumbListStack")

什么都不返回。 我想在:

中使用它
Storyboard.SetTarget(x, ThumbListBox.FindName("ThumbListStack"))

如何获取Stack Panel,以便我可以将其与TranslateTransform

一起使用

由于

3 个答案:

答案 0 :(得分:22)

您可以将Loaded事件用于StackPanel

中的ItemsPanelTemplate
<Grid>
    <Grid.Resources>
        <Style TargetType="ListBox">
            <Setter Property="ItemsPanel">
                <Setter.Value>
                    <ItemsPanelTemplate>
                        <StackPanel x:Name="ThumbListStack" Orientation="Horizontal"
                                    Loaded="StackPanel_Loaded" />
                    </ItemsPanelTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Grid.Resources>
    <ListBox />
</Grid>

然后在代码中

private StackPanel m_itemsPanelStackPanel;
private void StackPanel_Loaded(object sender, RoutedEventArgs e)
{
    m_itemsPanelStackPanel = sender as StackPanel;
}

另一种方法是遍历可视树并找到StackPanel,它将是ItemsPresenter的第一个孩子。

public void SomeMethod()
{
    ItemsPresenter itemsPresenter = GetVisualChild<ItemsPresenter>(listBox);
    StackPanel itemsPanelStackPanel = GetVisualChild<StackPanel>(itemsPresenter);
}

private static T GetVisualChild<T>(DependencyObject parent) where T : Visual
{
    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 = GetVisualChild<T>(v);
        }
        if (child != null)
        {
            break;
        }
    }
    return child;
}

答案 1 :(得分:3)

抱歉,我刚注意到我忘了保存编辑...我发现你已经接受了答案,但这对我来说似乎更像是一个黑客。这是我对FindChild的实现,您可能希望将来使用它,或者如果您经常这样做。

public static T FindChild<T>(this FrameworkElement obj, string name)
{
    DependencyObject dep = obj as DependencyObject;
    T ret = default(T);

    if (dep != null)
    {
        int childcount = VisualTreeHelper.GetChildrenCount(dep);
        for (int i = 0; i < childcount; i++)
        {
            DependencyObject childDep = VisualTreeHelper.GetChild(dep, i);
            FrameworkElement child = childDep as FrameworkElement;

            if (child.GetType() == typeof(T) && child.Name == name)
            {
                ret = (T)Convert.ChangeType(child, typeof(T));
                break;
            }

            ret = child.FindChild<T>(name);
            if (ret != null)
                break;
        }
    }
    return ret;
}

它会检查所有孩子和孩子的孩子,比较控件上设置的类型和名称。像这样使用它:

StackPanel ipanel = ThumbListBox.FindChild<StackPanel>("ThumbListStack");
if(ipanel == null)
    MessageBox.Show("couldn't find anything");
else
    MessageBox.Show("Aha! Found: " ipanel.Name);

答案 2 :(得分:1)

尝试以下扩展方法:

var childStackPanels = FindVisualChildren<StackPanel>(ThumbListBox);

方法本身:

public static IEnumerable<T> FindVisualChildren<T>(this DependencyObject depObj) where T : DependencyObject
{
    if (depObj != null)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
            var typedChild = child as T;
            if (typedChild != null)
            {
                yield return typedChild;
            }    

            foreach (T childOfChild in FindVisualChildren<T>(child))
            {
                yield return childOfChild;
            }
        }
    }
}

PS:您可以自己扩展它以检查特定的控件名称,因此方法将返回单个控件而不是列表。