在ListBox中查找控件? [英] Find Control Inside ListBox?

查看:292
本文介绍了在ListBox中查找控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < Style TargetType =ListBoxItemx:Key =ListBoxItemTemplate> 
< Setter Property =Template>
< Setter.Value>
< ControlTemplate TargetType =ListBoxItem>
< Button Content ={TemplateBinding Content}>< / Button>
< / ControlTemplate>
< / Setter>
< / style>
< ListBox ItemsSource ={Binding S}
x:Name =listBox
ItemContainerStyle ={StaticResource ListBoxItemTemplate}>
< ListBox.ItemsPanel>
< ItemsPanelTemplate>
< UniformGrid x:Name =gridColumns =5>< / UniformGrid>
< / ItemsPanelTemplate>
< /ListBox.ItemsPanel>
< / ListBox>

我想从ListBox Control中找到grid。请帮助我,谢谢。

解决方案

有几件事要补充到Meleak的答案中(而且这个评论太长了。)



通常情况下,从WPF中的模板中获取指定元素的方式是调用模板的FindName方法。但是,由于模板基本上是工厂,所以还需要说明需要哪个特定的模板实例 - 单个ItemsPanelTemplate可能已经实例化了几次。所以你需要这样的东西:

  var grid =(UniformGrid)listBox.ItemsPanel.FindName(grid,? ??); 

但是呢?占位?这不是ListBox本身 - ListBox实际上并不直接使用该ItemsPanel。最终,它被ListBox的模板中的ItemsPresenter使用。所以你需要这样做:
$ b $ pre $ var grid =(UniformGrid)listBox.ItemsPanel.FindName(grid,myItemsPresenter );

...除此之外,还没有可靠的方法来获取ItemsPresenter。实际上,甚至可能没有一个 - 为ListBox创建模板是合法的,它只是直接提供主机面板 - 甚至还有一个特殊的属性Panel.IsItemsHost,就是为了这个目的。

这导致了我想补充的第二点。在ListBox的模板不使用ItemsPresenter的情况下,ItemsPanel将不使用。所以实际上可能是你想要抓住的UniformGrid甚至不存在。


   <Style TargetType="ListBoxItem" x:Key="ListBoxItemTemplate">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Button Content="{TemplateBinding Content}"></Button>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>     
    <ListBox ItemsSource="{Binding S}" 
             x:Name="listBox" 
             ItemContainerStyle="{StaticResource ListBoxItemTemplate}">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid x:Name="grid" Columns="5"></UniformGrid>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    </ListBox>

I want to Find "grid" from ListBox Control.Please Help Me,Thank you.

解决方案

A couple of things to add to Meleak's answer (and this was a bit too long to put in a comment.)

Normally, the way you obtain a named element from a template in WPF is to call the template's FindName method. However, because templates are basically factories, you also needs to say which particular instance of the template you require - a single ItemsPanelTemplate may have been instantiated several times over. So you'd need something like this:

var grid = (UniformGrid) listBox.ItemsPanel.FindName("grid", ???);

But what goes in that ??? placeholder? It's not the ListBox itself - the ListBox doesn't actually use that ItemsPanel directly. Ultimately, the it's used by the ItemsPresenter in the ListBox's template. So you'd need to do this:

var grid = (UniformGrid) listBox.ItemsPanel.FindName("grid", myItemsPresenter);

...except, there's no reliable way to get hold of the ItemsPresenter either. In fact, there might not even be one - it's legal to create a template for a ListBox that just provides the hosting panel directly - there's even a special property, Panel.IsItemsHost, for this very purpose.

And that leads onto the second point I wanted to add. In scenarios where the ListBox's template doesn't use the ItemsPresenter, the ItemsPanel will go unused. So it's actually possible that the UniformGrid you're trying to get hold of doesn't even exist.

这篇关于在ListBox中查找控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆