从后面的代码中添加组合框项目.[WPF] [英] Add comboBox items from code behind. [WPF]
本文介绍了从后面的代码中添加组合框项目.[WPF]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从
XAML:
<Grid.ColumnDefinitions><ColumnDefinition Width="自动"/><ColumnDefinition Width="自动"/></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition Height="自动"/></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" ></组合框></网格>
解决方案
以编程方式设置项目:
代码隐藏:
private void PopulateComboBox(){cBox.ItemsSource = 新列表<字符串>{项目1",项目2",项目3"};}
XAML:
绑定到项目集合:
公共类 DummyClass{公共 int 值 { 得到;放;}公共字符串 DisplayValue { 获取;放;}}公共 ObservableCollection虚拟类集合{得到{返回新的 ObservableCollection{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:
<ComboBox Width="200" Height="30" x:Name="cBox" ItemsSource="{Binding DummyClassCollection}" DisplayMemberPath="DisplayValue"/>
I grabbed this code from MSDN. What Im trying to do is similar, but use a list instead of three different strings. so say
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?
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>
解决方案
Set the items programmatically:
Code-behind:
private void PopulateComboBox()
{
cBox.ItemsSource = new List<string> { "Item1", "Item2", "Item3"};
}
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:
<ComboBox Width="200" Height="30" x:Name="cBox" ItemsSource="{Binding DummyClassCollection}" DisplayMemberPath="DisplayValue" />
这篇关于从后面的代码中添加组合框项目.[WPF]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文