0

我的 xaml 文件中有 2 个组合框。基本上,当我们双击 xaml 文件中的组合框时,它会在 xaml.cs 文件中创建一个 combobox_selectionchanged 事件。我已经这样做了:

查看类:

<ComboBox Height="23" ItemsSource="{Binding BusRateList}" SelectedItem="{Binding SelectedBusRateItem}" SelectedIndex="2"  Name="comboBox2" SelectionChanged="comboBox2_SelectionChanged" />

<ComboBox Height="23" ItemsSource="{Binding BaudRateList}" SelectedItem="{Binding SelectedBaudRateItem}" SelectedIndex="6" Name="comboBox3" SelectionChanged="comboBox3_SelectionChanged" />

查看.xaml.cs 文件:

private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        int id = Convert.ToInt32(comboBox2.SelectedIndex);            
        int speed = mI2c._busRate[id]; //mI2C is object of viewmodel class

        sendBuf[0] = Convert.ToByte((speed & 0xFF000000) >> 24);
        sendBuf[1] = Convert.ToByte((speed & 0x00FF0000) >> 16);
        sendBuf[2] = Convert.ToByte((speed & 0x0000FF00) >> 8);
        sendBuf[3] = Convert.ToByte(speed & 0x000000FF);

        cmd = (256 << 8 | 0x00);
        mCom.WriteInternalCommand(cmd, 4, ref sendBuf);

        ReadBusAndBaudRate();            
    }


private void comboBox3_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        int id = Convert.ToInt32(comboBox3.SelectedIndex);            
        int speed = mI2c._baudRate[id]; //mI2C is object of viewmodel class
        sendBuf[0] = Convert.ToByte((speed & 0xFF000000) >> 24);
        sendBuf[1] = Convert.ToByte((speed & 0x00FF0000) >> 16);
        sendBuf[2] = Convert.ToByte((speed & 0x0000FF00) >> 8);
        sendBuf[3] = Convert.ToByte(speed & 0x000000FF);

        cmd = (256 << 8 | 0x00);
        mCom.WriteInternalCommand(cmd, 4, ref sendBuf);

        ReadBusAndBaudRate();
    }

public void ReadBusAndBaudRate()
    {          

        int speed = 100;

        // Some Code

        textBox1.Text = speed.ToString();

        textBox2.Text = speed.ToString();

        // Update message in Output Window as Effective Baud Rate
    }

视图模型类:

public ObservableCollection<int> _busRate;
public ObservableCollection<int> BusRateList
    {
        get { return _busRate; }
        set
        {
            _busRate = value;
            NotifyPropertyChanged("BusRateList");
        }
    }

    private int _selectedBusRate;
    public int SelectedBusRateItem
    {
        get { return _selectedBusRate; }
        set
        {
            _selectedBusRate = value;
            NotifyPropertyChanged("SelectedBusRateItem");
        }
    }      

    public ObservableCollection<int> _baudRate;
    public ObservableCollection<int> BaudRateList
    {
        get { return _baudRate; }
        set
        {
            _baudRate = value;
            NotifyPropertyChanged("BusRateList");
        }
    }

    private int _selectedBaudRate;
    public int SelectedBaudRateItem
    {
        get { return _selectedBaudRate; }
        set
        {
            _selectedBaudRate = value;
            NotifyPropertyChanged("SelectedBaudRateItem");
        }
    }

我在 viewmodel 构造函数的两个组合框中添加了大约 8 个项目。

现在使用上述属性,我想在 viewmodel 类中执行组合框选择更改事件,该事件应该执行在我的 .cs 文件中完成的所有语句。

请帮忙!!!

4

3 回答 3

3

如果您希望用户界面在没有选择更改事件的情况下更新您的视图模型,只需将双向绑定添加到您的选定项。这将使您进入具有您想要使用的值的视图模型。这是一个例子:

<ComboBox Height="23" ItemsSource="{Binding BusRateList}" SelectedItem="{Binding SelectedBusRateItem,Mode=TwoWay}" SelectedIndex="2"  Name="comboBox2"  /> 

编辑:您要执行的语句将在您的属性中,因此在 SelectedBusRate 中调用 SetBusRate(),如果 Busrate 可能受到影响,则调用 SelectedBaudRate。

于 2012-10-09T16:18:39.320 回答
1

使用 ReadBusAndBaudRate() 方法更新视图模型并在 SelectedItem 属性集中调用此方法

public int SelectedBusRateItem
    {
        get { return _selectedBusRate; }
        set
        {
            _selectedBusRate = value;
             ReadBusAndBaudRate();
            NotifyPropertyChanged("SelectedBusRateItem");
        }
    }
    private int _selectedBaudRate;
    public int SelectedBaudRateItem
    {
        get { return _selectedBaudRate; }
        set
        {
            _selectedBaudRate = value;
            ReadBusAndBaudRate();
            NotifyPropertyChanged("SelectedBaudRateItem");
        }
    }

    private int _speed;
    public int Speed
    {
        get { return _speed; }
        set
        {
            _speed = value;
            NotifyPropertyChanged("Speed");
        }
    }


    private void ReadBusAndBaudRate()
    {
        //Do some code
        Speed = 10; // will be your logical value.
        //For message notifications use MVVM frameworks such as cinch by Sacha Barber
    }
  • 使用绑定模式和文本框绑定更新 xaml 以提高速度

  • 将 mcom 的方法添加为

      public int SelectedBaudRateItem
        {
            get { return _selectedBaudRate; }
            set
            {
                _selectedBaudRate = value;                   
                WriteMcomCommand(_selectedBaudRate );
                ReadBusAndBaudRate();
                NotifyPropertyChanged("SelectedBaudRateItem");
            }
        }    
        private void WriteMcomCommand(int id)
        {
        int speed = mI2c._busRate[id]; //mI2C is object of viewmodel class
    
        sendBuf[0] = Convert.ToByte((speed & 0xFF000000) >> 24);
        sendBuf[1] = Convert.ToByte((speed & 0x00FF0000) >> 16);
        sendBuf[2] = Convert.ToByte((speed & 0x0000FF00) >> 8);
        sendBuf[3] = Convert.ToByte(speed & 0x000000FF);
    
        cmd = (256 << 8 | 0x00);
        mCom.WriteInternalCommand(cmd, 4, ref sendBuf);
        }
    
于 2012-10-09T16:28:04.597 回答
0

使用触发器和命令,例如

<ComboBox Height="23" ItemsSource="{Binding BusRateList}" SelectedItem="{Binding SelectedBusRateItem,Mode=TwoWay}" SelectedIndex="2"  Name="comboBox2"  >
  <i:Interaction.Triggers>
                    <i:EventTrigger EventName="SelectionChanged">
                    <Command:EventToCommand Command="{Binding SelectionChangedCommand}" PassEventArgsToCommand="True" />
                </i:EventTrigger>
                </i:Interaction.Triggers>
</ComboBox>

然后将视图模型中的命令连接到方法,例如

public ICommand SelectionChangedCommand { get; set; }

SelectionChangedCommand = new RelayCommand<SelectionChangedEventArgs>((e) => MyMethod(e));

您还需要将以下内容添加到 XAML 中的命名空间:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

如果你想使用 RelayCommands,你还需要 MVVM Lite。

于 2012-10-11T09:00:37.630 回答