My UserControl isn't being rendered when used

2019-09-06 08:29发布

问题:

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.