0
votes

I am trying to migrate a small prototype application I made in WinForms to WPF. I'm having some issues with a combobox in WPF not changing values when I select a different value from the drop-down. Initially, I tried just copying the code that I used in my WinForms app to populate the combobox and determine if a new index had been selected. This is how my WinForms code looked like:

private void cmbDeviceList_SelectedIndexChanged(object sender, EventArgs e)
{
    var cmb = (Combobox) sender;
    var selectedDevice = cmb.SelectedItem;
    var count = cmbDeviceList.Items.Count;

    // find all available capture devices and add to drop down
    for(var i =0; i<count; i++)
    {
        if(_deviceList[i].FriendlyName == selectedDevice.ToString())
        {
            _captureCtrl.VideoDevices[i].Selected = true;
            break;
        }
    }
}

Earlier in the code, I am populating the _deviceList List and the combo box (in Form1_Load to be specific) by looping over the available devices and adding them. I tried the same approach in WPF and could only populate the combo box. When I selected a new value, for some reason the same exact value (the initial device) was being sent into the event code (cmbCaptureDevices_SelectionChanged in my WPF app). I looked around for some tutorials in WPF and found that maybe data binding was my issue, and I tried that out instead. This is my combobox in my XAML file:

<ComboBox ItemsSource="{Binding Devices}" Name="cmbCaptureDevices" 
    IsSynchronizedWithCurrentItem="True" SelectedItem="{Binding CurrentDevice, 
    Mode=TwoWay}" Se;ectionChanged="cmbCapturedDevices_SelectionChanged" />

There's more to that XAML definition, but it's all arbitrary stuff like HorizontalAlignment and whatnot. My VideoDevicesViewModel inherits from INotifyPropertyChanged, has a private List<Device> _devices and a private Device _currentDevice. The constructor looks like:

public VideoDevicesViewModel()
{
    _devices = GetCaptureDevices();
    DevicesCollection = new CollectionView(_devices);
}

GetCaptureDevices simply is the loop that I had in my WinForms app which populates the list with all avaialble capture devices on the current machine. I have a public CollectionView DevicesCollection { get; private set; } for getting/setting the devices at the start of the application. The property for my current device looks like:

public Device CurrentDevice
{
    get { return _currentDevice; }
    set
    {
        if (_currentDevice = value)
        {
            return;
        }
        _currentDevice = value;
        OnPropertyChanged("CurrentDevice");
    }
}

OnPropertyChanged just raises the event PropertyChanged if the event isn't null. I'm new to WPF (and pretty new to C# in general, honestly) so I'm not sure if I'm missing something elementary or not. Any idea as to why this combobox won't change values for me?

3

3 Answers

0
votes

Discovered the answer on my own here. The unexpected behavior was a result of using the Leadtools Device class. It's a COM component and apparently was not playing nicely with my application. I honestly don't understand why exactly it worked, but I wrapped the Device class in another class and used that instead. As soon as I was using the wrapper class, the combo box functioned as it should.

0
votes

You are using the assignment operator '=' instead of the equality operator '=='

Change

if (_currentDevice = value) 

to

if (_currentDevice == value)
0
votes

Try the following

if _currentDevice == value ...