Disable ComboBox without changing appearance

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. However, this is for ComboBox does not prevent the user from choosing a different value.

+3
source share
7 answers

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

+8
source

, , ComboBox ControlTemplate ( ),

, 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>

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

, -, . , .

, IE 6/Kaxaml.

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <StackPanel>
  <ComboBox  Foreground="black" Background="white" IsEditable="True" Text="Hello" IsEnabled="false">
  <ComboBoxItem>Test1</ComboBoxItem>
  <ComboBoxItem>Test2</ComboBoxItem>
  <ComboBoxItem>Test3</ComboBoxItem>
  </ComboBox>
  </StackPanel>
</Page>
0

, .net, , VB6, , , ( , , ). . . , , .

0

comboBox.

- , , , .

combobox, , , . - , comboBox.

It just seems that in fact you have a summary on the screen with which people cannot interact when the shortcut works fine.

0
source

... prevent the user from choosing a different value.

In addition to style, you can disable keyboard input by overriding some ComboBox methods:

using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;

public class LockableComboBox : ComboBox
{

    protected override void OnSelectionChanged(SelectionChangedEventArgs e)
    {
        if (this.IsReadOnly)
        {
            e.Handled = true;
        }
        else
        {
            base.OnSelectionChanged(e);
        }
    }

    protected override void OnPreviewKeyDown(KeyEventArgs e)
    {
        if (this.IsReadOnly)
        {
            if ((e.Key == Key.C || e.Key == Key.Insert)
                && (Keyboard.Modifiers & ModifierKeys.Control) 
                == ModifierKeys.Control)
            {
                // Allow copy
                Clipboard.SetDataObject(SelectedValue, true);
            }
            e.Handled = true;
        }
        else
        {
            base.OnPreviewKeyDown(e);
        }
    }

    protected override void OnPreviewTextInput(TextCompositionEventArgs e)
    {
        if (this.IsReadOnly) 
        {
            e.Handled = true;
        }
        else
        {
            base.OnPreviewTextInput(e);
        }
    }

    protected override void OnKeyUp(KeyEventArgs e)
    {
        if (this.IsReadOnly)
        {
            e.Handled = true;
        }
        else
        {
            base.OnKeyUp(e);
        }
    }
}
0
source

Source: https://habr.com/ru/post/1708666/


All Articles