views:

222

answers:

2

I have a user control that allows items to be added to it by exposing a Grid's Children property. Any control I add shows up fine but when I try to bind a property on the added item to a control in the main window nothing happens (example):

<TextBox Name="txtTest" Text="Success!" />

<mycontrols:CustomUserControl.ExposedGridChildren>
     <TextBox Text="{Binding ElementName=txtTest, Path=Text, FallbackValue=fail}"/>
</mycontrols:CustomUserControl.ExposedGridChildren>

This example always results in the TextBox's text showing "fail". Here is how I'm exposing the children in the user control:

public UIElementCollection ExposedGridChildren
    {
        get { return grdContainer.Children; }
    }

Any thoughts? Is it a scope issue? I know I can't name the elements I add to the children because of scope errors. Thanks, Brian.

A: 

This might answer your question: http://stackoverflow.com/questions/1395701/how-do-you-bind-a-grids-children-to-a-list

Or as Dr. WPF puts it: (http://drwpf.com/blog/2007/10/15/itemscontrol-a-is-for-abundance/)

Is a Panel an ItemsControl?

No. The logical children of a panel are UIElements, whereas the logical children of an ItemsControl (its Items) can be any CLR objects.

Sidebar: So what is a panel? The main role of a panel is to provide layout support for its children. Although a panel does maintain a collection of child elements, it is not technically a WPF “control”… That is, it does not derive from the Control base class and it does not support the WPF notion of templating. Instead, it is a single element with a single purpose… namely, to size and position (a.k.a., measure and arrange) its children.

Tendlon
An itemscontrol seems to function exactly the same way. Perhaps I'm exposing it incorrectly? Now it's: public ItemCollection ExposedItemsControlItems{ get { return grdContainer.Items; } }I can still add controls and they show fine in the host window but I still can't bind outside the usercontrol.On a side note: I'm not binding a collection to the usercontrol I'm actually putting them into the xaml directly. Since I will be using this usercontrol over many forms I need the ability to add different controls to it on each form.
Brian
A: 

I was apparently going about this thing all wrong. What I needed to do was create a look-less control and template it to have one (or more) contentpresenter(s).

Brian