简体   繁体   中英

In WPF how do I bind a Content Property of the UserControl to the internal control

I Have a user Control which Contains a ScrollPanel. And I want to bind the userControl's content property to the ScrollPanel.

So my xaml would look like:

<CustomControl>
    <StackPanel/>
</CustomControl>

and in my UserControl my ScrollPanel child is set to StackPanel.

Do you mean ScrollViewer?

You have to remove the content from the user control (so the content no longer has a visual parent), then reassign the content to the scroll viewer.

In code:

var scrollViewer = new ScrollViewer();

var content = userControl.Content;
userControl.Content = null;     // removes content from visual tree
scrollViewer.Content = content;  // reassign content

If there's a way to do this via a binding, I haven't figured it out yet, though the situation where I'm having to do this is slightly different from yours.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM