绑定自定义类
关于 XAML 的简单问题。 有一个类 Achievement 和一个用于显示它的控件 AchievementControl。 我尝试创建一个包含 List 作为 ItemsSource 和 DataTemplate 的 ListBox,
<ListBox ItemsSource={Binding Achievements}>
<ListBox.ItemTemplate>
<DataTemplate>
<controls:AchievementControl Achievement="{Binding}" />
</DataTeplate>
</ListBix.ItemTemplate>
</ListBox>
但正如我在调试器中看到的那样,它将 System.Windows.Data.Binding 对象作为值发送到 Achievement 属性的 setter 而不是 Achievement 对象,因此执行失败。
Simple question about XAML.
There is a class Achievement and a control AchievementControl to display it.
I have tried to create a ListBox which contains List as ItemsSource and DataTemplate
<ListBox ItemsSource={Binding Achievements}>
<ListBox.ItemTemplate>
<DataTemplate>
<controls:AchievementControl Achievement="{Binding}" />
</DataTeplate>
</ListBix.ItemTemplate>
</ListBox>
But as I can see in debugger it sends a System.Windows.Data.Binding object as a value to setter of Achievement property instead of Achievement object, so, execution fails.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保包含 AchievementControl 的控件具有 DataContext 设置。然后确保 DataContext 有一个属性(示例 = MyAchievement)
Make sure your control that contains the AchievementControl has a DataContext set. Then make sure that DataContext has a Property (Example = MyAchievement)
如果您想直接与 Achievement 类绑定,那么您需要重写
ToString() 方法 或者创建一个值转换器或与类的属性绑定。
If you want to bind directly with the Achievement class then you need to override the
ToString() method Or create a value converter or bind with the properties of your class.