2012-05-06 29 views
0

我有應作爲容器的用戶控件,它看起來像如下:WPF容器控件

<ContentControl ... > 
    <Grid> 
     <Rectangle RadiusX="8" RadiusY="8"> 
      <Rectangle.Fill> 
... 
      </Rectangle.Fill> 
     </Rectangle> 

... 

     <ContentPresenter /> 
    </Grid> 
</ContentControl> 

當我在一個窗口中使用它,控件的內容被替換,而不是投入ContentPresenter:

<XWpf2:MyContainer Margin="40"> 
     <ListView Margin="16" /> 
</XWpf2:MyContainer> 

什麼是正確的方法,這裏有什麼問題?

+0

相似的問題:http://stackoverflow.com/q/10269284 –

回答

2

您將Content設置爲Grid,然後用ListView覆蓋Content。如果您希望保留該結構,則需要將其指定爲ContentControl.Template

當然,同樣的事情可以說對MyContainer如果是UserControl,如果設置Content,然後用ListView將其覆蓋你的整個ContentControl將離開爲好。

+0

當我把它放在ContentControl.Template中時,我得到了用戶控件的內容,但現在我看不到內容了? – Dusan

+0

@Dusan:[這個問題](http://stackoverflow.com/questions/5758342/)可能會有所幫助,你可能仍然在某處覆蓋某些東西。我無法推測你的代碼... –

+0

@HB - 解決了,似乎ContentPresenter必須在模板中綁定 - Content =「{Binding RelativeSource = {RelativeSource TemplatedParent},Path = Content}」。非常感謝! – Dusan