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

3 に答える 3

3

次のようにコンボボックスを変更します。

<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が、別のレイヤーにあると思われるクラスに を追加するのはアーキテクチャ上正しくない場合があります。

于 2013-03-29T12:34:25.360 に答える
1

「9600」をデフォルトのボーレートにするには、次の行を追加する必要があります

myComboBox.SelectedIndex = 7;

9600 は 7 位なので

それが役に立てば幸い...

于 2013-03-29T12:29:44.097 に答える
0

古いスレッドですが、私を正しい軌道に乗せました:

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>
于 2016-02-28T11:03:39.000 に答える