none
hide drop down button of combobox

Answers

  • Hi,

     

    The items in ComboBox are selectable, because the default item container for ComboBox is ComboBoxItem, which inherits a IsSelected property from ListBoxItem class. so them can be selected. If you just want to display data items, you can utilize the ItemsControl base class, which uses the ContentPresenter class as its default item container. In this case, the data items just used for displaying. If you were to manipulate the item container, you can overridet the ItemsControl or its proper descendants like ListBox, override GetContainerForItemOverride and IsItemItsOwnContainerOverride methods to define your own item container. The following is a small example.

     

     

    XAML code:

    <Window x:Class="WpfItemsContainerTest.Window1"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:s="clr-namespace:System;assembly=mscorlib"

        xmlns:local="clr-namespace:WpfItemsContainerTest"

        Title="Window1" Height="300" Width="300">

        <Window.Resources>

            <x:Array x:Key="data" Type="{x:Type s:String}">

                <s:String>Item1</s:String>

                <s:String>Item2</s:String>

                <s:String>Item3</s:String>

                <s:String>Item4</s:String>

                <s:String>Item5</s:String>

            </x:Array>

        </Window.Resources>

        <StackPanel DataContext="{StaticResource data}">

            <!--selectable-->

            <ListBox ItemsSource="{Binding}" />

            <!--unselectable-->

            <ItemsControl  ItemsSource="{Binding}" />

            <!--unselectable-->

            <local:CustomListBox   ItemsSource="{Binding}" />

        </StackPanel>

    </Window>

     

    In the code behind:

        public class CustomListBox : ListBox

        {

            protected override DependencyObject GetContainerForItemOverride()

            {

                return new ContentPresenter ();

            }

            protected override bool IsItemItsOwnContainerOverride(object item)

            {

                return (item is UIElement);

            }

    }

     

    Hope this helps.

    Thanks.


    Jim Zhou -MSFT
    Monday, June 08, 2009 6:43 AM

All replies

  • Hi Santosh BSK,

    you can "edit" the ControlTemplate for the ComboBox, but after removing the drop down button for me its no longer a ComboBox.

    Hope it helps.
    Sunday, May 31, 2009 4:15 PM
  • Hi,

     

    I suppose your meaning by the “drop down button” is the Popup control inside the default template of ComboBox. if you hide the Popup control, the ComboBox will no longer looks like ComboBox as Guenter Schwaiger said above.

     

    Thanks.

     


    Jim Zhou -MSFT
    Thursday, June 04, 2009 12:21 PM
  • I agree,

    I was trying to bind combox to items collection, but not selectable only displayable, so no drop down. I can do this with Listbox, but that was behaving not as expected. Anyways, i got it worked with some tweaks with list box. Thanks though.

    Thanks.
    Friday, June 05, 2009 5:51 PM
  • Hi,

     

    The items in ComboBox are selectable, because the default item container for ComboBox is ComboBoxItem, which inherits a IsSelected property from ListBoxItem class. so them can be selected. If you just want to display data items, you can utilize the ItemsControl base class, which uses the ContentPresenter class as its default item container. In this case, the data items just used for displaying. If you were to manipulate the item container, you can overridet the ItemsControl or its proper descendants like ListBox, override GetContainerForItemOverride and IsItemItsOwnContainerOverride methods to define your own item container. The following is a small example.

     

     

    XAML code:

    <Window x:Class="WpfItemsContainerTest.Window1"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:s="clr-namespace:System;assembly=mscorlib"

        xmlns:local="clr-namespace:WpfItemsContainerTest"

        Title="Window1" Height="300" Width="300">

        <Window.Resources>

            <x:Array x:Key="data" Type="{x:Type s:String}">

                <s:String>Item1</s:String>

                <s:String>Item2</s:String>

                <s:String>Item3</s:String>

                <s:String>Item4</s:String>

                <s:String>Item5</s:String>

            </x:Array>

        </Window.Resources>

        <StackPanel DataContext="{StaticResource data}">

            <!--selectable-->

            <ListBox ItemsSource="{Binding}" />

            <!--unselectable-->

            <ItemsControl  ItemsSource="{Binding}" />

            <!--unselectable-->

            <local:CustomListBox   ItemsSource="{Binding}" />

        </StackPanel>

    </Window>

     

    In the code behind:

        public class CustomListBox : ListBox

        {

            protected override DependencyObject GetContainerForItemOverride()

            {

                return new ContentPresenter ();

            }

            protected override bool IsItemItsOwnContainerOverride(object item)

            {

                return (item is UIElement);

            }

    }

     

    Hope this helps.

    Thanks.


    Jim Zhou -MSFT
    Monday, June 08, 2009 6:43 AM