开发者

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜