I have 2 comboboxes in my xaml file. Basically when we double click the combobox in xaml file, it creates a combobox_selectionchanged event in xaml.cs file. I have done it as follows:
View 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" />
View.xaml.cs file:
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 Class:
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");
}
}
I have added around 8 items in both comboboxes in viewmodel constructor.
Now Using the above properties I want to perform the combobox selection changed event in viewmodel class which should execute all the statements which were done in my .cs file.
Please help!!!
Update viewmodel with ReadBusAndBaudRate() method and call this method in set of SelectedItem properties
Update xaml with Binding Mode and TextBox binding for speed
Add method fo mcom stuff as