2016-06-17 32 views
-3

我想阻止用戶編輯/選擇組合框。我嘗試使用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> 
+0

請分享一下你的代碼 –

+0

的事實上,這將是很好的編輯您的問題,並添加到目前爲止你寫的代碼:如何創建一個最小的,完整的,和可驗證的例子?](http://stackoverflow.com/help/mcve)。 –

回答

0

我已通過將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 
0

如果你想防止用戶更改此組合框不使用只讀或啓用您可以在組合框SelectedIndexChanged事件中嘗試此操作。

但是沒有看到你的代碼,我們無法幫助解決特定的問題。

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

e是在選擇所選索引更改事件時傳入的事件參數。