在DataTemplate中使用我的UserControl时未呈现 [英] My UserControl isn't being rendered when used inside a DataTemplate
问题描述
我在MyUserControl.xaml中定义了一个UserControl。如果我直接从另一个控件引用它,那么它将正确显示。例如:
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>
可以正常工作。但是,如果我尝试在DataTemplate中使用UserControl,它将无法正常工作。例如,如果我有一个数据模板,例如
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>
什么都不会呈现。
怎么了?
推荐答案
您的UserControl是否有相应的.xaml.cs文件?
Does your UserControl have a corresponding .xaml.cs file? This seems to happen when it doesn't.
如果使用的是Visual Studio,请尝试从UserControl复制所有XAML,然后将其从项目中删除,然后添加一个新的UserControl(与以前的名称相同),然后将您的内容粘贴回XAML文件中。这样可以确保您设置了正确的.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.
这篇关于在DataTemplate中使用我的UserControl时未呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!