gpt4 book ai didi

wpf - 获取数据模板中的数据项

转载 作者:行者123 更新时间:2023-12-01 12:52:07 27 4
gpt4 key购买 nike

有没有办法获取 DataTemplate 的实际 DataItem。更具体地说,我有一个在数据模板中使用的自定义按钮:

<DataTemplate x:Key="SampleDataTemplate1">
<custom:SampleButton />
</DataTemplate>

我在 ListView 中使用它来绑定(bind)到一个集合。我想传递对被绑定(bind)的实际 DataItem 的引用。像这样:

<DataTemplate x:Key="SampleDataTemplate1">
<custom:SampleButton BoundItem="{Binding DataItem}" />
</DataTemplate>

这可能吗?如何实现?

最佳答案

您可以绑定(bind)到数据模板中使用的数据。这是一个例子:

<DataTemplate x:Key="SampleDataTemplate1">
<custom:SampleButton BoundItem="{Binding}" />
</DataTemplate>

此处有更多详细信息(请参阅指定值的路径部分):

http://msdn.microsoft.com/en-us/library/ms752347.aspx#creating_a_binding

想法是,在数据模板内部,其 DataContext 中的所有元素都引用数据模板绑定(bind)到的项目。没有 Path 的 {Binding} 构造简单地绑定(bind)到 DataContext。

关于wpf - 获取数据模板中的数据项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11614030/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com