如何在ViewModel类中执行ComboboxSelectionChanged方法

时间:2012-10-09 16:01:34

标签: c# wpf mvvm combobox viewmodel

我的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" />

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

ViewModel类:

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文件中完成的所有语句。

请帮助!!!

3 个答案:

答案 0 :(得分:3)

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

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

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

答案 1 :(得分:1)

使用ReadBusAndBaudRate()方法更新viewmodel并在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
    }
  • 使用绑定模式和TextBox绑定更新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);
        }
    

答案 2 :(得分: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 =&#34; HTTP://schemas.microsoft.com/expression/2010/interactivity"

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