2009-07-24 236 views
2

我有一個組合框,只有少量顯示爲圖像和文本(每個項目並排放置)的項目。現在,當我從組合框列表中選擇一個項目時,我想要顯示其他文本(而不是相同的圖像和文本),可能是Combobox selecteditem區域中的另一個文本或另一個圖像。WPF組合框選定項目

有沒有一種方法可以實現它。

+0

我想改變appearence了when他組合框倒塌,將selectedItem顯示在組合框。 – deepak 2009-07-24 09:49:22

回答

2

最簡單的方法是將一個IsSelected觸發器添加到Combobox的DataTemplate(Itemstemplate),我認爲您有兩組可視元素用於常規數據顯示,另一組用於所選視覺效果,當IsSelected屬性設置爲on您需要的ComboboxItem將常規視覺效果隱藏起來,並顯示另一個視覺效果。這裏真正的技巧是找到立即使用FindAncestor選擇的用戶ComboBoxItem

<DataTemplate x:Key="yourDataTemplate"> 
<Grid x:Name="regularVisuals" > ... </Grid> 
<Grid x:Name="selectedVisuals" Visibility="Collapsed"> ... </Grid> 
<DataTemplate.Triggers> 
    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ComboBoxItem}},Path=IsSelected}" Value="True"> 
     <Setter TargetName="regularVisuals" Property="Visibility" Value="Visible"/> 
     <Setter TargetName="selectedVisuals" Property="Visibility" Value="Collapsed"/> 
    </DataTrigger> 
</DataTemplate.Triggers> 

+0

兩個** Setter **的** TargetName **是否應該真正閱讀** selectedVisuals **?不應該是** regularVisuals **? – Mizipzor 2009-10-15 14:52:02

相關問題