如何为数据绑定组合框预定义组合框项目?

时间:2013-03-29 12:10:31

标签: c# wpf xaml data-binding combobox

我想允许用户选择串口的波特率。 我创建了一个带有串口波特率的文本框,如下所示,它可以工作。

<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. 这看起来并不那么优雅。完成这项工作的最佳方法是什么?

  3. 供参考,这是我的串口类。像上面的代码一样难看。我使用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
    }
    

3 个答案:

答案 0 :(得分: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;
  

注意:这样你可以绑定你的COMBOBOX值,但是在ObservableCollection添加到似乎在另一层的类中可能在架构上是不正确的。

答案 1 :(得分:1)

将“9600”作为添加行

所需的默认波特率
myComboBox.SelectedIndex = 7;

9600位于第7位

希望它有所帮助...

答案 2 :(得分:0)

旧帖子让我走上正轨:

通过添加SelectedValuePath =&#34; Content&#34;解决它,并将其保存到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>
相关问题