在运行时刷新组合框项目 wpf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14568984/
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
refresh combobox items at runtime wpf
提问by Santa Dan Dumitru
I want a comboBox containing open serial ports as items. But it updates automatically when serial ports modify.
我想要一个包含开放串行端口作为项目的组合框。但是当串口修改时它会自动更新。
My approach is this:
我的方法是这样的:
create in Xaml a comboBox like this:
<ComboBox Height="23" HorizontalAlignment="Left" Margin="87,35,0,0" Name="comboBox1" VerticalAlignment="Top" Width="147" ContextMenuOpening="comboBox1_ContextMenuOpening" />create a method that loops trough ports and adds them to comboBox:
public string[] portsManual; public void adaugaPorturi() { if (comboBox1.Items.Count > 0) { comboBox1.Items.RemoveAt(0); } comboBox1.Items.Add("Select port"); comboBox1.SelectedItem = "Select port"; portsManual = SerialPort.GetPortNames(); foreach (string port in portsManual) { comboBox1.Items.Add(port); } comboBox1.Items.Refresh(); Array.Clear(portsManual, 0, portsManual.Length); }call this method at window.load (so it can have something in it - the ports opened right then) and call it at ContextMenuOpening event (whenever I click the comboBox to select an item - a port, to refresh)
在 Xaml 中创建一个像这样的组合框:
<ComboBox Height="23" HorizontalAlignment="Left" Margin="87,35,0,0" Name="comboBox1" VerticalAlignment="Top" Width="147" ContextMenuOpening="comboBox1_ContextMenuOpening" />创建一个通过端口循环并将它们添加到组合框的方法:
public string[] portsManual; public void adaugaPorturi() { if (comboBox1.Items.Count > 0) { comboBox1.Items.RemoveAt(0); } comboBox1.Items.Add("Select port"); comboBox1.SelectedItem = "Select port"; portsManual = SerialPort.GetPortNames(); foreach (string port in portsManual) { comboBox1.Items.Add(port); } comboBox1.Items.Refresh(); Array.Clear(portsManual, 0, portsManual.Length); }在 window.load 调用这个方法(所以它可以有一些东西 - 然后打开端口)并在 ContextMenuOpening 事件中调用它(每当我点击组合框选择一个项目 - 一个端口,刷新)
Note: I am clearing the comboBox items (if any) at start, and the same for the string array, but it doesnt update, the items are still the ones at first run of this method!
注意:我在开始时清除组合框项目(如果有),字符串数组也是如此,但它没有更新,这些项目仍然是第一次运行这个方法时的项目!
I have tried: ObservableCollection, strings, and arrays with no luck. The most info I have found on this site. I am thinking that because are only a few ports, I don't really need a Collection or List. I just add items directly.
我尝试过:ObservableCollection、字符串和数组,但没有运气。我在这个网站上找到的最多的信息。我在想,因为只有几个端口,我真的不需要集合或列表。我只是直接添加项目。
BTW, can an admin edit this post? I am not sure if text format is good.
顺便说一句,管理员可以编辑这篇文章吗?我不确定文本格式是否好。
采纳答案by Wes Cumberland
First off, you should probably be using MVVM and data binding rather than querying serial ports directly from your view's code-behind.
首先,您可能应该使用 MVVM 和数据绑定,而不是直接从视图的代码隐藏中查询串行端口。
Secondly, it seems like you're using the wrong event, if you want it to refresh everytime the menu opens, you should use the DropDownOpened event
其次,您似乎使用了错误的事件,如果您希望每次打开菜单时都刷新它,则应该使用 DropDownOpened 事件
<ComboBox Height="23" HorizontalAlignment="Left" Margin="87,35,0,0" Name="comboBox1" VerticalAlignment="Top" Width="147" DropDownOpened="comboBox1_DropDownOpened" />
And try this in your code-behind
并在您的代码隐藏中尝试此操作
public void InitPorts()
{
RefreshPorts();
comboBox1.SelectedItem = "Select port";
}
public void RefreshPorts()
{
comboBox1.Items.Clear();
comboBox1.Items.Add("Select port");
foreach (var port in SerialPort.GetPortNames())
comboBox1.Items.Add(port);
}
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
InitPorts();
}
private void ComboBox1_OnDropDownOpened(object sender, EventArgs e)
{
RefreshPorts();
}

