在Combobox中添加新元素,下拉列表未更新

时间:2013-01-30 22:58:13

标签: c# wpf xaml

我基于代码隐藏绑定ComboBox,下面是我的XAML和Code Behind:

<Window x:Class="ComboBoxBindingDatabase.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <ComboBox Height="29" HorizontalAlignment="Left" Margin="135,60,0,0" Name="comboBox1" VerticalAlignment="Top" Width="194" IsEditable="True"/>
    <TextBox Height="39" HorizontalAlignment="Left" Margin="135,159,0,0" Name="textBox1" VerticalAlignment="Top" Width="194" />
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="384,165,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    <TextBox Height="35" HorizontalAlignment="Left" Margin="368,0,0,172" Name="textBox2" VerticalAlignment="Bottom" Width="115" />
</Grid>

代码背后:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        BindComboBox(comboBox1);
    }

    ArrayList b = new ArrayList();

    public void BindComboBox(ComboBox comboBoxName)
    {

        b.Add("a");
        b.Add("b");
        b.Add("c");
        b.Add("d");
        comboBoxName.ItemsSource = b;  
        comboBoxName.DisplayMemberPath = Name;
        comboBoxName.SelectedValuePath = Name;

    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        b.Add(textBox2.Text);
        foreach (string s in b)
        {
            comboBox1.ItemsSource = b;
            comboBox1.DisplayMemberPath = Name;
            comboBox1.SelectedValuePath = Name;
        }

    }

}

所以当我第一次运行项目时,ComcoBox会显示a,b,c d作为下拉列表。但是当我在文本框中输入一些新字母并单击按钮时,ArrayList b会将新字母添加到列表中,我正在尝试重新绑定组合框,但它不起作用。它仍然显示a,b,c,d,我认为我看到ArrarList b会增量。

为什么ComboBox没有重新绑定?无论如何,根据Code Behind动态地将元素添加到ComboBox中,而不仅仅是item.add方法。

提前致谢。

2 个答案:

答案 0 :(得分:2)

您应该使用ObservableCollection。并且您不必在按钮单击内再次绑定,即使每当添加新项目并且组合框将被更新时,集合将触发更改通知。

  ObservableCollection<string> b= new ObservableCollection<string>();

  public void BindComboBox(ComboBox comboBoxName)
    {

        b.Add("a");
        b.Add("b");
        b.Add("c");
        b.Add("d");

        comboBoxName.ItemsSource = b;  


    }


 private void button1_Click(object sender, RoutedEventArgs e)
    {
        b.Add(textBox2.Text);

    }

答案 1 :(得分:0)

在WPF中,直接在后面的代码中引用UIControls是不好的做法。 您的控件应该绑定到Data,当您更改Data时,应该更新UI。

以下是一个小例子,说明你对UIControls的引用要少得多吗

的Xaml:

<Window x:Class="WpfApplication8.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="233" Width="405" Name="UI">
    <Grid DataContext="{Binding ElementName=UI}">
        <StackPanel>
            <ComboBox ItemsSource="{Binding MyList}" Name="comboBox1" IsEditable="True"/>
            <TextBox x:Name="textbox1" />
            <Button Content="Button" Tag="{Binding ElementName=textbox1, Path=Text}" Name="button1" Click="button1_Click" />
        </StackPanel>
    </Grid>
</Window>

代码:

  public partial class MainWindow : Window
    {
        private ObservableCollection<string> _myList = new ObservableCollection<string>();

        public MainWindow()
        {
            InitializeComponent();
            MyList.Add("a");
            MyList.Add("b");
            MyList.Add("c");
            MyList.Add("d");
        }

        public ObservableCollection<string> MyList
        {
            get { return _myList; }
            set { _myList = value; }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            MyList.Add((sender as Button).Tag.ToString());
        }
     }