![](/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.