Jonas
Jonas

Reputation: 311

WPF DockPanel: cannot select it to add children

I have created a few text boxes that have to be added as children to my DockPanel. I have given this DockPanel a name and then when I try in my code to use this name, Intellisense does not give me any results. How do I select DockPanel and then add its children?

I want to select it and add some text boxes to it as its children. For example: nameLocationPanel.Children.Add(new TextBox());

This is the top part of my XAML code that contains the DockPanel. It already has some text boxes, but I want to add more in my C# code.

        <ListBox ItemContainerStyle="{StaticResource lbStyle}" Name="searchList" BorderBrush="#FF898989" BorderThickness="2" MouseUp="searchList_MouseUp" MouseDoubleClick="searchList_MouseDoubleClick" Visibility="Visible">
            <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <DockPanel Name="nameLocationPanel">
                                <TextBlock FontSize="14" HorizontalAlignment="Left" Text="{Binding Path=FirstName}"/>
                                <TextBlock FontSize="14" HorizontalAlignment="Left" Text=" "/>
                                <TextBlock FontSize="14" HorizontalAlignment="Left" Text="{Binding Path=LastName}"/>
                                <TextBlock HorizontalAlignment="Right" Text="{Binding Path=Location}"/>
                            </DockPanel>

Upvotes: 1

Views: 701

Answers (2)

Koopakiller
Koopakiller

Reputation: 2883

You cannot access to the DockPanel because it is a part of a DataTemplate. It is generated for every item in your ListBox so it is impossible to access to a single instance of it.

To add additional items to the DockPanel it is necessary to do it via something like a binding in XAML or a special UserControl. But it depend always on the ListBoxItem.

Upvotes: 1

Ronan Thibaudau
Ronan Thibaudau

Reputation: 3603

It is within a DataTemplate, you can't select it by name at all as it doesn't exist in your Window, it's just a Template definition that will get repeated so there isn't 1 dockpanel but N, N being equal to how many items there are in your listbox and there could well be 0 dockpanels. What exactly are you trying to achieve? Odds are you're going about it backward.

Upvotes: 1

Related Questions