My UserControl isn't being rendered when used inside a DataTemplate

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 18:39:53

问题


I have a UserControl defined in MyUserControl.xaml. If I reference this directly from another control, then it displays correctly. For example:

<Grid>
    <MyUserControl/>
<Grid>

works as expected. But if I try to use the UserControl in a DataTemplate it doesn't work. For example, if I have a DataTemplate like

<DataTemplate>
   <MyUserControl/>
<DataTemplate>

nothing gets rendered.

What's up?


回答1:


Does your UserControl have a corresponding .xaml.cs file? This seems to happen when it doesn't.

If you're using Visual Studio, try copying all the XAML from your UserControl, then deleting it from your project, adding a new UserControl (with the same name as before), then pasting your content back into the XAML file. This will ensure that you have the correct .xaml.cs file set up.



来源:https://stackoverflow.com/questions/26058717/my-usercontrol-isnt-being-rendered-when-used-inside-a-datatemplate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!