c# - 如何在 ViewModel 类中执行 ComboboxSelectionChanged 方法

标签 c# wpf mvvm combobox viewmodel

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

查看类(class):

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

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

请帮忙!!!

最佳答案

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

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

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

关于c# - 如何在 ViewModel 类中执行 ComboboxSelectionChanged 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12803968/

相关文章:

c# - WPF ComboBox : static list of ComboBoxItems, 但数据绑定(bind) SelectedItem?

c# - 将 TextBox 绑定(bind)到 ObservableCollection.Count WPF?

c# - 如何在 .net 中的主 UI 线程以外的线程上运行 UI?

c# - 为什么 XDocument.Load() 和 XmlDocument.LoadXml() 无法加载注释包含两个以上 "--"符号的 XML?

c# - 是否有可能通过反射获得属性(property)的私有(private)二传手?

c# - 如何从 asp.net webform 中的 gridview 优化 "foreach"事件?

WPF 绑定(bind)到自定义控件的属性

c# - Combobox SelectedItem 数据绑定(bind)空引用异常

c# - 使用 MVVM lightMessenger 时,应用程序调用了为不同线程编码的接口(interface)

windows-phone-7 - 无法创建 "ViewModelLocator"的实例