ユーザーがシリアル ポートのボー レートを選択できるようにしたいと考えています。以下のようにシリアルポートのボーレートにバインドされたテキストボックスを作成しましたが、動作します。
<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
}