ComboBox problem! How to show a part of SelectedItem.....

This is the ComboBox:

<ComboBox Width="200" Height="30">
<ComboBoxItem>

<WrapPanel>
<MenuItem Width="20" IsChecked="True" ></MenuItem>

<TextBlock >Hello</TextBlock>
</WrapPanel>

</ComboBoxItem>


<ComboBoxItem>

<WrapPanel>
<MenuItem Width="20" IsChecked="True" ></MenuItem>

<TextBlock >World</TextBlock>
</WrapPanel>

</ComboBoxItem>
</ComboBox>

When I choose one item, the whole item is showed after DropDownClosed

But I don't want to show the checked menuitem.....

What I want to show is TextBlock's Text.

Is there any method to realize it

Thank U.



Answer this question

ComboBox problem! How to show a part of SelectedItem.....

  • MikeCampbell

    You can use a DataTemplateSelector on combo box to pick a different template for selection area.

    Here is a sample selector:

    public class MyTemplateSelector : DataTemplateSelector
    {
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
    ContentPresenter presenter = (ContentPresenter)container;

    if (presenter.TemplatedParent is ComboBox)
    return (DataTemplate)presenter.FindResource("SelectionTemplate");
    else // Templated parent is ComboBoxItem
    return (DataTemplate)presenter.FindResource("DropDownTemplate");
    }
    }

    XAML:

    <Window x:Class="WindowsApplication.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WindowsApplication"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    SizeToContent="WidthAndHeight">

    <ComboBox Width="200" Height="30">
    <ComboBox.ItemTemplateSelector>
    <local:MyTemplateSelector/>
    </ComboBox.ItemTemplateSelector>

    <ComboBox.Resources>
    <!-- Template applied to items in drop down-->
    <DataTemplate x:Key="DropDownTemplate">
    <WrapPanel>
    <CheckBox Width="20" IsChecked="True"/>
    <TextBlock Text="{Binding}"/>
    </WrapPanel>
    </DataTemplate>

    <!-- Template applied to main area -->
    <DataTemplate x:Key="SelectionTemplate">
    <TextBlock Text="{Binding}"/>
    </DataTemplate>
    </ComboBox.Resources>

    <sys:String>Hello</sys:String>
    <sys:String>World</sys:String>

    </ComboBox>
    </Window>



  • ComboBox problem! How to show a part of SelectedItem.....