从后面的代码中添加组合框项目。[WPF]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31562145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add comboBox items from code behind. [WPF]
提问by Gisiota
I grabbed this code from MSDN. What Im trying to do is similar, but use a list instead of three different strings. so say
我从MSDN 中获取了这段代码。我试图做的是类似的,但使用一个列表而不是三个不同的字符串。这么说
List<string> strList = new List<string>();
strList.Add("Created with C#");
strList.Add("Item 2");
strList.Add("Item 3");
//MSDN CODE BELOW
cbox = new ComboBox();
cbox.Background = Brushes.LightBlue;
cboxitem = new ComboBoxItem();
cboxitem.Content = "Created with C#";
cbox.Items.Add(cboxitem);
cboxitem2 = new ComboBoxItem();
cboxitem2.Content = "Item 2";
cbox.Items.Add(cboxitem2);
cboxitem3 = new ComboBoxItem();
cboxitem3.Content = "Item 3";
cbox.Items.Add(cboxitem3);
cv2.Children.Add(cbox);
Tried to do cbox.Items.Add(strList); Also tried a forloop to loop through each element, but that doesn't work either. Any ideas how I can achieve this?
试图做 cbox.Items.Add(strList); 还尝试了一个 forloop 来循环遍历每个元素,但这也不起作用。任何想法如何实现这一目标?
XAML:
XAML:
<Grid x:Name="grid44" DataContext="{StaticResource tBLPERMITSViewSource}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="409">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Content="SPR PACKET ASSIGMENT" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="0" VerticalAlignment="Center" FontWeight="Bold"/>
<ComboBox x:Name="sPR_ASSIGNEDComboBox" Grid.Column="1" DisplayMemberPath="SPR_ASSIGNED" HorizontalAlignment="Left" Height="Auto" Text="{Binding SPR_ASSIGNED}" ItemsSource="{Binding Items}" Margin="3,5,-114.35,5" Grid.Row="0" VerticalAlignment="Center" Width="238.35" Background="White" IsReadOnly="True" IsEditable="True" >
</ComboBox>
</Grid>
回答by User92
Set the items programmatically:
以编程方式设置项目:
Code-behind:
代码隐藏:
private void PopulateComboBox()
{
cBox.ItemsSource = new List<string> { "Item1", "Item2", "Item3"};
}
XAML:
XAML:
<ComboBox Width="200" Height="30" x:Name="cBox" />
Bind to a collection of items:
绑定到项目集合:
public class DummyClass
{
public int Value { get; set; }
public string DisplayValue { get; set;}
}
public ObservableCollection<DummyClass> DummyClassCollection
{
get
{
return new ObservableCollection<DummyClass>
{
new DummyClass{DisplayValue = "Item1", Value = 1},
new DummyClass{DisplayValue = "Item2", Value = 2},
new DummyClass{DisplayValue = "Item3", Value = 3},
new DummyClass{DisplayValue = "Item4", Value = 4},
};
}
}
XAML:
XAML:
<ComboBox Width="200" Height="30" x:Name="cBox" ItemsSource="{Binding DummyClassCollection}" DisplayMemberPath="DisplayValue" />
回答by CalusV
You can use data binding. Data binding allows you to bind the dynamic data from your list to the combobox and have it generated and filled with the content you are passing through.
您可以使用数据绑定。数据绑定允许您将列表中的动态数据绑定到组合框,并生成它并填充您正在传递的内容。
回答by Daniel
If you don't want to do this following a binding/mvvm pattern, it is quite simple to just do the following:
如果您不想按照绑定/mvvm 模式执行此操作,只需执行以下操作非常简单:
foreach (var item in items)
{
_comboBox.Items.Add(item);
_comboBox.SelectedValuePath = "ID";
_comboBox.DisplayMemberPath = "Name";
}
Which can be accessed later like so:
稍后可以像这样访问:
var id = _comboBox.SelectedValue;


