2013-03-29 163 views
2

我想讓用戶選擇串口的波特率。 我創建了一個帶有串口波特率綁定的文本框,如下所示。如何預定義數據綁定組合框的組合框項?

<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> 

雖然這個工程,我有幾個問題。

  1. 當我第一次加載窗口時,未選擇波特率的默認值(9600)。

  2. ,這並不顯得那麼優雅。完成這個的最好方法是什麼?

作爲參考,這裏是我的串口類。像上面的代碼一樣醜陋。我使用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 
} 

回答

1

爲 「9600」 是你需要添加行

myComboBox.SelectedIndex = 7; 

默認波特率爲9600,在第7位

希望它可以幫助...

+0

這個工程。謝謝。 –

+0

是否可以從綁定中獲取默認值? –

+0

您正在對默認值進行硬編碼,而不是以編程方式綁定它,因此您應該已經知道默認值。或者我沒有理解你的查詢.. – GeekyCoder

3

像這樣改變你的Combobox:

<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; 

注:這樣你可以綁定你的組合框的值,但是這可能是不正確架構向ObservableCollection增加,這似乎是在另一個層的類。

+0

是否可以省略SelectedIndex =「6」?我可以從綁定中獲取值嗎? –

+0

你是否有建議爲此創建類的正確方法?一個關鍵字應該讓我開始。 –

+0

@publicENEMY是的,可以省略它。這取決於你使用的是什麼架構(MVP,MVVM或...)。然後你可以定義額外的課程並完成你的工作。但是從我的答案中可以看出整個綁定的想法。 –

0

舊線程,但讓我在正確的軌道上:

加入SelectedValuePath =「內容」,並將其保存到的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>