【问题标题】:How to predefine combobox items for a data binded combobox?如何为数据绑定组合框定义组合框项?
【发布时间】:2013-03-29 12:10:31
【问题描述】:

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

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

【问题讨论】:

    标签: c# wpf xaml data-binding combobox


    【解决方案1】:

    像这样改变你的组合框:

    <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 添加到似乎位于另一层的类中可能在架构上不正确。

    【讨论】:

    • 是否可以省略 SelectedIndex="6"?我可以从绑定中获取值吗?
    • 您对为此目的创建类的正确方法有什么建议吗?一个关键字应该让我开始。
    • @publicENEMY 是的,可以省略它。这取决于您使用的架构(MVP、MVVM 或...)。然后您可以定义其他类并完成您的工作。但是从我的回答中可以看出绑定的整个想法。
    • 您在 XAML 和代码中都设置了 ItemsSource。也没有理由引入新的ObservableCollection&lt;int&gt;,除非列表中的项目可能发生变化。
    • @BrentStewart 我多次更新代码,所以发生了这些事情。在ObservableCollection 的情况下,您是对的,但也许他想进行一些更改,我想向他展示更一般的情况。
    【解决方案2】:

    要使“9600”成为默认波特率,您需要添加该行

    myComboBox.SelectedIndex = 7;
    

    因为 9600 排在第 7 位

    希望对你有帮助...

    【讨论】:

    • 是否可以通过绑定获取默认值?
    • 您正在硬编码默认值,而不是以编程方式绑定它,因此您应该已经知道默认值。还是我不明白你的查询..
    • 我没有硬编码默认值。我知道默认值,因为来自显示默认绑定的文本框绑定。当我尝试使用组合框绑定时,默认值不显示。
    【解决方案3】:

    老话题,但让我走上了正轨:

    通过添加 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-26
      • 2016-05-02
      • 2012-01-24
      • 1970-01-01
      • 2017-11-16
      • 2013-04-23
      • 1970-01-01
      • 2011-02-01
      相关资源
      最近更新 更多