tags:

views:

1384

answers:

5

Is there a way to disable changing the value of a ComboBox in WPF without giving it the visual properties of a disabled ComboBox? For example, I know that for a text field you can set the IsReadOnly property to true. Doing this for a ComboBox however, does not prevent the user from selecting a different value.

A: 

Are you sure this is a good idea from a usability/conventions standpoint? If your goal is readability, perhaps you can change the disabled color to bump up the contrast a bit.

http://social.msdn.microsoft.com/forums/en-US/wpf/thread/45dd7614-326b-4a51-b809-d25a3ff1ade8/

Anyway, I suspect you could write an onChange event handler to reset the value to the previous entry.

steamer25
The reason for this is due to the fact that it's a preexisting screen that merely needs to have some functionality dropped, but I'm wanting to keep the same look and feel. There are a select few users for this portion of the application and I've already done some walkthroughs over the screen to get a feel for exactly how they use it and what they expect to see.
Phillip Benages
A: 

You can set the Foreground and Background colors and that seems to override the disabled colors. The drop down button shows as disabled which is good.

EDIT My code I tested with in IE 6/Kaxaml.

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
  <StackPanel>
  <ComboBox  Foreground="black" Background="white" IsEditable="True" Text="Hello" IsEnabled="false">
  <ComboBoxItem>Test1</ComboBoxItem>
  <ComboBoxItem>Test2</ComboBoxItem>
  <ComboBoxItem>Test3</ComboBoxItem>
  </ComboBox>
  </StackPanel>
</Page>
Chris Persichetti
Just tried this and it did not appear to make a difference.
Phillip Benages
Sorry it didn't help. I updated the full file just in case that helps.
Chris Persichetti
A: 

Im not sure if its the same in .net however back in the VB6 days i use to get a picture box, frame or other container (sorry off the top of my head i can't remember which). I would put the combobox within that. To the users it looks the same. When you disable the container this would lock out the combo box as well, leaving it looking normal.

Audioillity
+2  A: 

While I agree that a disabled control should look disabled you could just set the ComboBox ControlTemplate to the standard one (or one your using) removing any of the standard functionality

eg This will give you a decent looking readonly combobox

  <ComboBox>
    <ComboBox.Template>
        <ControlTemplate TargetType="{x:Type ComboBox}">
          <Grid>
            <Microsoft_Windows_Themes:ListBoxChrome x:Name="Border" Height="23" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" RenderFocused="{TemplateBinding IsKeyboardFocusWithin}" RenderMouseOver="{TemplateBinding IsMouseOver}"/>
            <TextBlock FontSize="{TemplateBinding FontSize}" VerticalAlignment="Center" Text="Selected Item" Margin="5,0,0,0"></TextBlock>
          </Grid>
      </ControlTemplate>
    </ComboBox.Template>
  </ComboBox>

you'll need to include the following namespace

xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
PaulB
+2  A: 

Mr. Benages, I think setting IsHitTestVisible and Focusable to false on the ComboBox might do the trick. Hope this helps.

Thomas Visic
This did the trick. Thank you very much for your assistance.
Phillip Benages