David
David

Reputation: 185

Access to embedded WPF controls in xaml

Lets say I got the following user control:

<UserControl x:Class="MyUserControl">
    <tk:DataGrid>
        <tk:DataGrid.Columns>
            <!-- some columns -->
        </tk:DataGrid.Columns>
    </tk:DataGrid>
</UserControl>

Actually, I can't define what columns the data grid should have, since I need to use this control in many places, with different columns. Here is what I want to do:

<UserControl x:Class="MyPanel">
    <ui:MyUserControl>
        <Columns>
            <!-- columns that will go into the data grid -->
        </Columns>
    </ui:MyUserControl>
</UserControl>

Is it possible to achieve this?

P.S: DataGrid.Columns is readonly, it is not possible to bind it to something else.

Upvotes: 1

Views: 554

Answers (2)

Adi Lester
Adi Lester

Reputation: 25211

As @H.B. said in his solution, you should expose your own dependency property, but just performing binding wouldn't work, since the Columns property is read-only.

What you need to do is handle this in your dependency property's OnChange callback and add/remove columns as necessary. You'll also need to register to your dependency property's CollectionChanged event and possibly the grid's Columns CollectionChanged as well to synchronize the two properties.

Unfortunately, I don't think there's a XAML-only solution for this.

Upvotes: 1

brunnerh
brunnerh

Reputation: 185280

You can expose a depenency property on the UserControl called Columns which can bind to an internal DataGrid. You would access the property via <ui:MyUserControl.Columns> of course.

Upvotes: 2

Related Questions