Getting selecteditem from a customized WPF combobox
I'm building a combobox in WPF that has a ContentPresenter and Image in a stack panel. T开发者_如何学Gohe combobox items show up fine. What I'd like to do now is be able to get the content value in the contentpresenter from the selected item programatically using C#.
How would I go about doing this? Thanks in advance.
Here is my XAML:
<ComboBox x:Name="cbo1" Width="140" TextBlock.FontSize="12">
<ComboBoxItem>
<StackPanel Orientation="Horizontal">
<ContentPresenter Width="90" TextBlock.FontSize="12" HorizontalAlignment="Left" Content="Item1"/>
<Image Width="16" Height="11" Source="Images\Item1.png"/>
</StackPanel>
</ComboBoxItem>
</ComboBox>
This snippet should do it
var item = cbo1.SelectedItem as ComboBoxItem;
var stackpanel = item.Content as StackPanel;
var selectedContent = (stackpanel.Children[0] as ContentPresenter).Content;
精彩评论