当我在WPF窗口中添加了一个comboBox,如何向其中添加项?是在设计的XAML代码中还是在NameOfWindow.xaml.cs文件中进行操作?
当我在WPF窗口中添加了一个comboBox,如何向其中添加项?是在设计的XAML代码中还是在NameOfWindow.xaml.cs文件中进行操作?
情况一 - 没有数据源:
您可以按照以下方式使用静态值填充ComboBox
-
<ComboBox Height="23" Name="comboBox1" Width="120">
<ComboBoxItem Content="Alice"/>
<ComboBoxItem Content="Bob"/>
<ComboBoxItem Content="Charlie"/>
</ComboBox>
private void Window_Loaded(object sender, RoutedEventArgs e)
{
comboBox1.Items.Add("Alice");
comboBox1.Items.Add("Bob");
comboBox1.Items.Add("Charlie");
}
// insert item at specified index of populated ComboBox
private void Window_Loaded(object sender, RoutedEventArgs e)
{
comboBox1.Items.Insert(2, "Alice");
comboBox1.Items.Insert(5, "Bob");
comboBox1.Items.Insert(8, "Charlie");
}
情况2 - 您有一个数据源,且项目永远不会更改:
您可以使用数据源来填充ComboBox
。任何IEnumerable
类型都可以用作数据源。您可以 -
XAML
中的ItemsSource
属性绑定到数据源,例如 -<!-- MyDataSource is an IEnumerable type property in ViewModel -->
<ComboBox Height="23" Width="120" ItemsSource="{Binding MyDataSource}" />
ItemsSource
属性,例如 -private void Window_Loaded(object sender, RoutedEventArgs e)
{
comboBox1.ItemsSource = new List<string> { "Alice", "Bob", "Charlie" };
}
CASE 3 - 数据源可能会发生改变
ObservableCollection<T>
作为数据源XAML
中将ItemsSource
属性绑定到数据源(如上所示)ItemsSource
属性(如上所示)使用ObservableCollection<T>
确保每当向数据源添加或删除项目时,更改将立即反映在UI上。您可以自己决定如何填充ObservableCollection<T>
。
最好构建ObservableCollection并利用它的优势
public ObservableCollection<string> list = new ObservableCollection<string>();
list.Add("a");
list.Add("b");
list.Add("c");
this.cbx.ItemsSource = list;
cbx是组合框的名称
使用此代码
string[] str = new string[] {"Foo", "Bar"};
myComboBox.ItemsSource = str;
myComboBox.SelectedIndex = 0;
或者
foreach (string s in str)
myComboBox.Items.Add(s);
myComboBox.SelectedIndex = 0;
我认为comboBox1.Items.Add("X");
会将string
添加到ComboBox,而不是ComboBoxItem
。
正确的解决方案是
ComboBoxItem item = new ComboBoxItem();
item.Content = "A";
comboBox1.Items.Add(item);
有很多方法可以执行此任务。这里是一个简单的方法:
<Window x:Class="WPF_Demo1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="TestWindow"
Title="MainWindow" Height="500" Width="773">
<DockPanel LastChildFill="False">
<StackPanel DockPanel.Dock="Top" Background="Red" Margin="2">
<StackPanel Orientation="Horizontal" x:Name="spTopNav">
<ComboBox x:Name="cboBox1" MinWidth="120"> <!-- Notice we have used x:Name to identify the object that we want to operate upon.-->
<!--
<ComboBoxItem Content="X"/>
<ComboBoxItem Content="Y"/>
<ComboBoxItem Content="Z"/>
-->
</ComboBox>
</StackPanel>
</StackPanel>
<StackPanel DockPanel.Dock="Bottom" Background="Orange" Margin="2">
<StackPanel Orientation="Horizontal" x:Name="spBottomNav">
</StackPanel>
<TextBlock Height="30" Foreground="White">Left Docked StackPanel 2</TextBlock>
</StackPanel>
<StackPanel MinWidth="200" DockPanel.Dock="Left" Background="Teal" Margin="2" x:Name="StackPanelLeft">
<TextBlock Foreground="White">Bottom Docked StackPanel Left</TextBlock>
</StackPanel>
<StackPanel DockPanel.Dock="Right" Background="Yellow" MinWidth="150" Margin="2" x:Name="StackPanelRight"></StackPanel>
<Button Content="Button" Height="410" VerticalAlignment="Top" Width="75" x:Name="myButton" Click="myButton_Click"/>
</DockPanel>
</Window>
private void myButton_Click(object sender, RoutedEventArgs e)
{
ComboBoxItem cboBoxItem = new ComboBoxItem(); // Create example instance of our desired type.
Type type1 = cboBoxItem.GetType();
object cboBoxItemInstance = Activator.CreateInstance(type1); // Construct an instance of that type.
for (int i = 0; i < 12; i++)
{
string newName = "stringExample" + i.ToString();
// Generate the objects from our list of strings.
ComboBoxItem item = this.CreateComboBoxItem((ComboBoxItem)cboBoxItemInstance, "nameExample_" + newName, newName);
cboBox1.Items.Add(item); // Add each newly constructed item to our NAMED combobox.
}
}
private ComboBoxItem CreateComboBoxItem(ComboBoxItem myCbo, string content, string name)
{
Type type1 = myCbo.GetType();
ComboBoxItem instance = (ComboBoxItem)Activator.CreateInstance(type1);
// Here, we're using reflection to get and set the properties of the type.
PropertyInfo Content = instance.GetType().GetProperty("Content", BindingFlags.Public | BindingFlags.Instance);
PropertyInfo Name = instance.GetType().GetProperty("Name", BindingFlags.Public | BindingFlags.Instance);
this.SetProperty<ComboBoxItem, String>(Content, instance, content);
this.SetProperty<ComboBoxItem, String>(Name, instance, name);
return instance;
//PropertyInfo prop = type.GetProperties(rb1);
}
注意:这是使用反射。 如果您想了解有关反射基础知识以及为什么要使用它的更多信息,这是一篇很好的介绍性文章:
如果您想了解如何在WPF中使用反射,这里有一些资源:
如果您想大幅度提高反射的性能,最好使用IL来实现,像这样:
使用OleDBConnection -> 连接到Oracle
OleDbConnection con = new OleDbConnection();
con.ConnectionString = "Provider=MSDAORA;Data Source=oracle;Persist Security Info=True;User ID=system;Password=**********;Unicode=True";
OleDbCommand comd1 = new OleDbCommand("select name from table", con);
OleDbDataReader DR = comd1.ExecuteReader();
while (DR.Read())
{
comboBox_delete.Items.Add(DR[0]);
}
con.Close();
就这些了 :)