我有一个UserControl,我在其中添加了一个Dependency Property:
public partial class WordControl : UserControl
{
// Using a DependencyProperty as the backing store for WordProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty WordProperty =
DependencyProperty.Register("WordProperty", typeof(string), typeof(WordControl), new FrameworkPropertyMetadata("", OnWordChange));
public string Word
{
get { return (string)GetValue(WordProperty); }
set { SetValue(WordProperty, value); }
}
当我在XAML中手动设置Word属性时,它可以正常工作:
<WordGame:WordControl Word="Test"></WordGame:WordControl>
然而,我希望将此UserControl作为ListBox的Item Template的一部分,并使用Data Binding来设置单词:
<ListBox Name="WordList" IsEnabled="False" IsHitTestVisible="False">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<WordGame:WordControl Word="{Binding}"></WordGame:WordControl>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
当我运行它时,会产生以下错误:
“绑定”无法设置在类型为“WordControl”的“Word”属性上。只能在DependencyObject的DependencyProperty上设置“绑定”。
由于UserControl继承自DependencyObject,因此我只能假设问题出在DependencyProperty上,但是无论如何搜索都没有找到答案。
有什么想法吗?