I have two ComboBox
on my code:
<ComboBox Name="comboBoxSelectCamera" ItemsSource="{Binding Path=ListCameras}" SelectionChanged="comboBoxSelectCamera_SelectionChanged" />
<ComboBox Name="comboBoxCities" ItemsSource="{Binding Path=ListCities}" />
On my Window
I have this code so the ComboBox
understand where the Path
comes from:
<Window ....
DataContext="{Binding RelativeSource={RelativeSource Self}}"
.... >
Both combos are binded to two listes that I created on my MainWindow
:
public MainWindow()
{
InitializeCitiesCombo();
InitializeComponent();
// Initialize the control that checks for cameras
InitializeCameraControl();
FillCameraProperties();
DataContext = this;
}
The first ComboBox
list is created before the InitializeComponent
, so when its combo is created, it fills with the specific content.
The second ComboBox
list is created after the InitializeComponent
, because it depend on an object that loads the cameras, and, I don't know if that's the reason, but if the list is created after InitializeComponent
, it doesn't fill the ComboBox
.
I know there are other ways to fill a list of connected cameras on my computer, but I have to use that control that is created on the InitializeComponent
and initiated on the InitializeCameraControl()
.
Any idea on how can I fill this ComboBox
after the InitializeComponent
?
EDIT: Declaration of both Lists:
private List<CameraInfo> ListCameras { get; set; }
private List<String> ListCities { get; set; }
Somewhere in code, the ListCites
:
ListCities = new List<String> { Madrid, Barcelona, Alicante, Valencia }
Somewhere in code, my control returns a list of the connected cameras (it works, it has elements but they don't display on the combo):
ListCameras = MyUserControl.ConnectedCameras; // this returns a valid list of `CameraInfo`
My CameraInfo
class:
public class CameraInfo
{
public CameraInfo(string name, string id);
public static implicit operator string(CameraInfo cameraInfo);
public string Id { get; }
public string Name { get; }
public override string ToString();
}
Note that while we discuss this, I tried to change on the combo of cameras the DisplayMemberPath
.
Binding error:
System.Windows.Data Error: 40 : BindingExpression path error: 'Text' property not found on 'object' ''MainWindow' (Name='')'. BindingExpression:Path=Text; DataItem='MainWindow' (Name=''); target element is 'TextBox' (Name=''); target property is 'Template' (type 'ControlTemplate')