开发者

Getting selecteditem from a customized WPF combobox

开发者 https://www.devze.com 2023-01-30 03:56 出处:网络
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 th

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;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号