【问题标题】:How to change foreground of combobox on disabled state?如何在禁用状态下更改组合框的前景?
【发布时间】:2024-04-23 09:55:02
【问题描述】:

我想阻止用户编辑/选择组合框。我尝试使用cmbbox.IsReadOnly = Truecmbbox.IsEditable = False,它允许用户通过altarrow' keys 更改选择。 cmbbox.isEnabled = False 有效,我的要求是在禁用组合框前景色时将其更改为“黑色”。谁能帮我解决一下?

在 XAML 中:

<telerik:RadComboBox Grid.Row="0" Grid.Column="2" Grid.ColumnSpan="2"  x:Name="combobox1" IsEditable="True" IsFilteringEnabled="True" ItemsSource="{Binding}" TabIndex="7" Style="{ DynamicResource DropDownListStyle }" IsTabStop="True" KeyboardNavigation.TabNavigation ="Local" SelectAllTextEvent="None" Height="23" Margin="0,0,0,2" VerticalAlignment="Center"/>

在代码隐藏中:

combobox1.IsEnabled = False

风格:

 <Style x:Name="DropDownListStyle" x:Key="DropDownListStyle" TargetType="telerik:RadComboBox" >
                <Setter Property="Foreground" Value="#FF000000"/>
                <Setter Property="BorderBrush" Value="#ffcccccc"/>
                <Setter Property="BorderThickness" Value="1"/>
                <Setter Property="HorizontalContentAlignment" Value="Left" />
                <Setter Property="VerticalContentAlignment" Value="Center" />
                <Setter Property="FontSize" Value="12"/>
                <Setter Property="FontWeight" Value="Thin"/>
                <Setter Property="FontFamily" Value="Trebuchet MS"/>
                <Setter Property="Panel.ZIndex" Value="10" />
                <Setter Property="Height" Value="23" />
                <!--  <Setter Property="Focusable" Value="True"/> -->
                <Style.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Background" Value="White"/>
                        <Setter Property="Foreground" Value="Black"/>
                    </Trigger>
                </Style.Triggers>
            </Style>

【问题讨论】:

标签: wpf vb.net xaml radcombobox


【解决方案1】:

如果您想阻止用户在不使用 ReadOnly 或 Enabled 的情况下更改此组合框,您可以在 Combo Box SelectedIndexChanged 事件上尝试此操作。

但如果没有看到您的代码,我们无法解决特定问题。

'Inform the user
MsgBox("You can't change this drop down")
'Reset any choice
e.NewValue = e.CurrentValue

e 是选择所选索引更改事件时传入的事件参数。

【讨论】:

    【解决方案2】:

    我已通过将IsReadonly = True 属性设置为combobox 并触发PreviewKeyDown 事件来修复它。

    combobox1.IsReadonly = True
    

    Private Sub combobox1_PreviewKeyDown(sender As Object, e As Windows.Input.KeyEventArgs) Handles combobox1.PreviewKeyDown
            If combobox1.IsReadOnly Then
                If e.Key = Key.Tab Then
                    e.Handled = False
                Else
                    e.Handled = True
                End If
            End If
    End Sub
    

    【讨论】: