在DataTemplate中使用时,我的UserControl未呈现

时间:2022-06-24 16:46:18

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

我在MyUserControl.xaml中定义了UserControl。如果我直接从另一个控件引用它,那么它会正确显示。例如:

<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中使用UserControl,它就不起作用。例如,如果我有一个像DataTemplate

<DataTemplate>
   <MyUserControl/>
<DataTemplate>

nothing gets rendered.

什么都没有渲染。

What's up?

这是怎么回事?

1 个解决方案

#1


1  

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

您的UserControl是否有相应的.xaml.cs文件?这似乎发生在没有的时候。

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.

如果您正在使用Visual Studio,请尝试从UserControl复制所有XAML,然后从项目中删除它,添加新的UserControl(与之前的名称相同),然后将内容粘贴回XAML文件。这将确保您设置正确的.xaml.cs文件。

#1


1  

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

您的UserControl是否有相应的.xaml.cs文件?这似乎发生在没有的时候。

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.

如果您正在使用Visual Studio,请尝试从UserControl复制所有XAML,然后从项目中删除它,添加新的UserControl(与之前的名称相同),然后将内容粘贴回XAML文件。这将确保您设置正确的.xaml.cs文件。