如何禁用组合框中的项目之一,即wpf中的字符串集合? [英] How to disable one of items in a combo box which is collection of strings in wpf?
本文介绍了如何禁用组合框中的项目之一,即wpf中的字符串集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在组合框中禁用项目之一,该组合框中是wpf中的字符串集合.
How to disable one of items in a combo box which is collection of strings in wpf
推荐答案
您好,
将项目列表添加为组合框项目
Hi,
Add the list of items as Comboboxitem
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" Name="Window1">
<Grid>
<ComboBox Height="23" Margin="56,55,102,0" Name="ComboBox1" VerticalAlignment="Top" >
<ComboBoxItem Content="Item1"/>
<ComboBoxItem Content="Item2" IsEnabled="False" />
</ComboBox>
<ComboBox Margin="76,121,82,118" Name="ComboBox2">
<ComboBox.ItemTemplate>
<DataTemplate>
<ComboBoxItem Content="{Binding Name}" IsEnabled="{Binding IsToBeEnabled}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
</Window>
隐藏代码:
Code Behind:
Class Window1
Private Sub Window1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
Dim lst As New List(Of MyData)
Dim obj As New MyData
obj.IsToBeEnabled = True
obj.Name = "Item1"
lst.Add(obj)
obj = New MyData
obj.Name = "Item2"
obj.IsToBeEnabled = False
lst.Add(obj)
obj = New MyData
obj.Name = "Item3"
obj.IsToBeEnabled = True
lst.Add(obj)
ComboBox2.ItemsSource = lst
End Sub
End Class
Class MyData
Private blnIsToBeEnabled As Boolean
Public Property IsToBeEnabled() As Boolean
Get
Return blnIsToBeEnabled
End Get
Set(ByVal value As Boolean)
blnIsToBeEnabled = value
End Set
End Property
Private strName As String
Public Property Name() As String
Get
Return strName
End Get
Set(ByVal value As String)
strName = value
End Set
End Property
End Class
在这里,我有两个combobox.其中一个直接在xaml本身中添加了comboboxitems.如果要迭代后面代码中的字符串列表,请创建一个comboboxitem并将其添加到comboboxitem
在第二个combobox中我已经覆盖了模板,并基于绑定对象的属性,我正在设置comboboxitem的IsEnabled
希望对您有所帮助
Here i have two combobox.One has directly comboboxitems added in xaml itself.If you want you can iterate the list of string in code behind,create a comboboxitem and add it to the comboboxitem
In the second combobox i have overridden the template and based on a property of the object binded i am setting the comboboxitem's IsEnabled
Hope it helps
这篇关于如何禁用组合框中的项目之一,即wpf中的字符串集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文