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开发者_JAVA百科 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.
精彩评论