我想讓用戶選擇串口的波特率。 我創建了一個帶有串口波特率綁定的文本框,如下所示。如何預定義數據綁定組合框的組合框項?
<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
}
這個工程。謝謝。 –
是否可以從綁定中獲取默認值? –
您正在對默認值進行硬編碼,而不是以編程方式綁定它,因此您應該已經知道默認值。或者我沒有理解你的查詢.. – GeekyCoder