![](/img/trans.png)
[英]How do I call Button command from inside of ListBox.ItemTemplate DataTemplate, to pass parameter without selecting ListBox item?
[英]Customizing a Listbox - How to pass a Listbox Reference to a Converter from within an ItemTemplate?
我有一个具有Name
和Value
属性的对象列表,这些对象显示在列表框中。 该列表按Name
排序,我需要在Listbox
的第三列中显示所有先前对象的值之和。
我从其中调用ListItemConverter
位置创建了一个ItemTemplate
,该ListItemConverter
应该通过访问父级Listbox
将任何ListItem
转换为所需的double
值。 我可以将当前的ListItem
(或相应的ContentPresenter
)传递给我的转换器
RelativeSource="{RelativeSource TemplatedParent}"
但是ListItem不包含整个Listbox
的信息(当我以这种方式声明时,这似乎很明显),因此我还需要将对我的Listbox
(或用作ItemSource
的列表)的引用传递给Converter 。
如何将第二个参数传递给转换器,如何从ItemTemplate
中将父Listbox
作为资源访问?
您有两种选择:
在您的项目类中创建第三个属性,或使用Name,Value和Sum属性创建新的Item类。 特别是在mvvm方案中使用此功能
使用MultiConverter和MultiBinding:
<ListBox ItemsSource="{Binding MyCollection}"> <ListBox.ItemTemplate> <DataTemplate> <TextBlock> <TextBlock.Text> <MultiBinding Converter="{StaticResource YourMultiConverter}"> <Binding /> <Binding Path="ItemsSource" RelativeSource="{RelativeSource AncestorType=ListBox}" /> </MultiBinding> </TextBlock.Text> </TextBlock> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.