1

在我后面的代码中,我将 MessageBoxTabControl.ItemsSource 设置为 Observable Collection。

<TabControl x:Name="MessageBoxTabControl">
    <TabControl.ContentTemplate>
        <DataTemplate>
            <ListBox x:Name="MessageListBox" />
                <!-- ^ I want a reference to this control -->
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>

假设我有相关的 tabcontrol 和 tabitem,我怎样才能获得对我的 ListBox 的引用?

4

2 回答 2

4

你有没有想过以其他方式做任何你想做的事情?通常,当您拥有 DataTemplate 时,您可能希望在该模板内的控件上设置的任何属性都应该是静态的(所以为什么要访问它们)或取决于提供的数据,然后应该由 DataBinding 实现。

您可以使用以下代码获取 ListBox。我仍然觉得重新考虑你的结构而不是使用这段代码会更好。

xml:

<Window x:Class="WpfApplication1.MainWindow"
                    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    Title="MainWindow" Height="350" Width="525">

    <TabControl x:Name="MessageBoxTabControl">
        <TabControl.ContentTemplate>
            <DataTemplate >
                <ListBox x:Name="MessageListBox" >
                    <ListBoxItem Content="ListBoxItem 1" /> <!-- just for illustration -->
                </ListBox>
            </DataTemplate>
        </TabControl.ContentTemplate>
        <TabItem Header="Tab 1" />
        <TabItem Header="Tab 2" />
    </TabControl>
</Window>

后面的代码:

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    ListBox lbx = FindVisualChildByName<ListBox>(this.MessageBoxTabControl, "MessageListBox");
    if (lbx != null)
    {
        // ... what exactly did you want to do ;)?
    }
}

private T FindVisualChildByName<T>(DependencyObject parent, string name) where T : FrameworkElement
{
    T child = default(T);
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
    {
        var ch = VisualTreeHelper.GetChild(parent, i);
        child = ch as T;
        if (child != null && child.Name == name)
            break;
        else
            child = FindVisualChildByName<T>(ch, name);

        if (child != null) break;
    }
    return child;
}

还有第二种类似的方式,它实际上使用了模板,但仍然依赖于可视化树来获取 ContentPresenter(FindVisualChild 实现类似于上面):

ContentPresenter cp = FindVisualChild<ContentPresenter>(this.MessageBoxTabControl);
ListBox lbx = cp.ContentTemplate.FindName("MessageListBox", cp) as ListBox;

请注意,由于对可视树的这种依赖性,您将始终只能使用此方法找到所选选项卡的 ListBox。

于 2013-04-10T21:48:27.530 回答
0

应该是这样的:

TabItem relevantTabItem = howeverYouGetThisThing();
var grid = System.Windows.Media.VisualTreeHelper.GetChild(relevantTabItem, 0);
var listBox = (ListBox) System.Windows.Media.VisualTreeHelper.GetChild(grid, 0);
于 2013-04-10T18:05:50.363 回答