Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Coders,

In my WPF application we are using styles for ComboBox.

This is the style we are using for ComboBox.

XML
<Style x:Key="ComboBoxToggleButton150" TargetType="ToggleButton">
            <Setter Property="OverridesDefaultStyle" Value="true" />
            <Setter Property="IsTabStop" Value="false" />
            <Setter Property="Focusable" Value="false" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ToggleButton">
                        <chrome:ButtonChrome x:Name="Chrome"
                                    BorderBrush="{TemplateBinding BorderBrush}" 
                                    removed="{TemplateBinding Background}"
                                    CornerRadius="0"
                                    RenderEnabled="{TemplateBinding IsEnabled}"
                                    RenderMouseOver="{Binding IsMouseOver, ElementName=PART_DropDownButton}"
                                    RenderNormal="False"
                                    RenderPressed="{Binding IsPressed, ElementName=PART_DropDownButton}"
                                    SnapsToDevicePixels="True">
                            <Grid HorizontalAlignment="Right" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}">
                                <Path x:Name="Arrow" Data="{StaticResource DownArrowGeometry}" Fill="Black" HorizontalAlignment="Center" Margin="3,0,3,0" VerticalAlignment="Center" />
                            </Grid>
                        </chrome:ButtonChrome>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter Property="RenderPressed" TargetName="Chrome" Value="True" />
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Fill" TargetName="Arrow" Value="#AFAFAF" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="ComboBoxStyle" TargetType="ComboBox">
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" />
            <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" />
            <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}" />
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
            <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
            <Setter Property="Padding" Value="2" />
            <Setter Property="ScrollViewer.CanContentScroll" Value="true" />
            <Setter Property="ScrollViewer.PanningMode" Value="Both" />
            <Setter Property="Stylus.IsFlicksEnabled" Value="False" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ComboBox">
                        <Grid x:Name="MainGrid" SnapsToDevicePixels="true">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0" />
                            </Grid.ColumnDefinitions>

                            
<Popup x:Name="PART_Popup" AllowsTransparency="true" Grid.ColumnSpan="2" IsOpen="{TemplateBinding IsDropDownOpen}" Margin="1" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom">
                                <Grid MinWidth="{Binding ActualWidth, ElementName=MainGrid}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
                                    <Border x:Name="DropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" 
                                            BorderThickness="1" removed="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" >
                                        <ScrollViewer x:Name="DropDownScrollViewer" >
                                            <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                                        </ScrollViewer>
                                    </Border>
                                </Grid>
                            </Popup>
                            <Border Grid.ColumnSpan="2" removed="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" />
                           
                            <TextBox x:Name="PART_EditableTextBox" Text="{TemplateBinding Text}" IsReadOnly="False" BorderThickness="0" removed="Transparent" Margin="{TemplateBinding Padding}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" Width="170" />
                            <ToggleButton x:Name="PART_DropDownButton" 
                                          Grid.Column="1" 
                                          BorderBrush="{TemplateBinding BorderBrush}" 
                                          removed="{TemplateBinding Background}" 
                                           IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                                          Style="{StaticResource ComboBoxToggleButton150}"
                                IsHitTestVisible="{Binding Path=IsDropDownOpen,RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource BooleanToVisibilityConverter}}" ClickMode="Press"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEditable"
               Value="true">
                                <Setter Property="IsTabStop" Value="false"/>
                                <Setter TargetName="PART_EditableTextBox" Property="Visibility"  Value="Visible"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>


Now the requirement is we need to do a non editable combobox.

How can i alter this above code to make non editable combobox.

Please Be feel free and give me the solution plese.
Posted
Updated 4-Apr-13 2:21am
v2
Comments
Sergey Alexandrovich Kryukov 4-Apr-13 9:56am    
Not clear. "Non-editable combobox" means yourComboBox.IsReadOnly = true; what's the problem?
—SA
Irina Pykhova 4-Apr-13 11:33am    
"non-editable" usually means that you can select some item from combobox, but can't type text
Sergey Alexandrovich Kryukov 4-Apr-13 11:43am    
I would not be so sure. Selection is used in ComboBoxes as data input. But yes, it makes sense to have such mode, which is also used...
—SA

1 solution

change IsReadOnly to True for PART_EditableTextBox
 
Share this answer
 
Comments
dmunisubbu 4-Apr-13 23:02pm    
Thank you so much Irina.
You saved my time

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900