我的 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 文件中完成的所有语句。
请帮忙!!!