如何为数据绑定组合框预定义组合框项
本文关键字:组合 预定义 数据绑定 | 更新日期: 2023-09-27 18:06:23
我想让用户选择串行端口的波特率。我创建了一个文本框绑定串口波特率如下,它的工作。
<TextBox x:Name="tbbaudRate" Text="{Binding SerialPort.BaudRate}" />
我的问题是,有有限的有效波特率集。有效的波特率为{75、110、300、1200、2400、4800、9600、19200、38400、57600、115200}。我想将文本框更改为列出有效波特率值的组合框。
我是这样做的。
<ComboBox x:Name="tbbaudRate" Text="{Binding SerialPort.BaudRate}" >
<ComboBoxItem Content="75"/>
<ComboBoxItem Content="110"/>
<ComboBoxItem Content="300"/>
<ComboBoxItem Content="1200"/>
<ComboBoxItem Content="2400"/>
<ComboBoxItem Content="4800"/>
<ComboBoxItem Content="9600"/>
<ComboBoxItem Content="19200"/>
<ComboBoxItem Content="38400"/>
<ComboBoxItem Content="57600"/>
<ComboBoxItem Content="115200"/>
</ComboBox>
当这个工作时,我有几个问题。
当我第一次加载窗口时,波特率没有选择默认值(9600)。
这看起来不那么优雅。实现这一目标的最佳方式是什么?
供参考,这里是我的串口类。也像上面的代码一样丑陋。我使用resharper来自动生成notifypropertychange代码。
class SerialComm : INotifyPropertyChanged
{
private int[] ValidBaudRate = new[] { 75, 110, 300, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200 }; //Dont know how to use this
private int[] ValidDataBits = new[] { 5, 6, 7, 8, 9 }; //Dont know how to use this
private SerialPort _serialPort;
public SerialComm()
{
_serialPort = new SerialPort();
}
public SerialPort SerialPort
{
get { return _serialPort; }
set
{
_serialPort = value;
OnPropertyChanged("SerialPort");
SerialPort.GetPortNames();
}
}
#region Autogenerate by resharper
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
像这样改变你的组合框:
<ComboBox Name="comboBox1" Width="120"
ItemsSource="{Binding Path=ValidBaudRateCollection}">
<ComboBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding }"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
将这些添加到您的SerialComm
类:
public ObservableCollection<int> ValidBaudRateCollection;
public SerialComm()
{
this.ValidBaudRateCollection = new ObservableCollection<int>(this.ValidBaudRate);
_serialPort = new SerialPort();
}
最后将这些添加到您的Window
(例如构造函数)的某个地方
SerialComm s = new SerialComm();
comboBox1.DataContext = s;
comboBox1.ItemsSource = s.ValidBaudRateCollection;
comboBox1.SelectedIndex = 6;
注意:这种方式你可以绑定你的COMBOBOX值,但它可能是不正确的架构添加一个
ObservableCollection
类,似乎是在另一个层。
为"9600"作为默认波特率,您需要添加
行myComboBox.SelectedIndex = 7;
作为9600在第7位
老帖子,但让我走上了正确的轨道:
通过添加SelectedValuePath="Content"并保存到SelectedValue来解决。
<ComboBox
SelectedValue="{Binding LaserBaudRate, UpdateSourceTrigger=PropertyChanged}"
SelectedValuePath="Content">
<ComboBoxItem Content="75" />
<ComboBoxItem Content="110" />
<ComboBoxItem Content="300" />
<ComboBoxItem Content="1200" />
<ComboBoxItem Content="2400" />
<ComboBoxItem Content="4800" />
<ComboBoxItem Content="9600" />
<ComboBoxItem Content="19200" />
<ComboBoxItem Content="38400" />
<ComboBoxItem Content="57600" />
<ComboBoxItem Content="115200" />
</ComboBox>