我observableCollection
在 C# 代码中有一个属性并ComboBox
绑定到这个可观察的集合:
<ComboBox x:Name="CameraInstances" Margin="10,0,0,0" Width="250" StaysOpenOnEdit="True" VerticalAlignment="Center" VerticalContentAlignment="Center"
ItemsSource="{Binding Path=CameraSettingsManager.Observable, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
SelectedIndex="{Binding Path=CameraSettingsManager.EditingCameraIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectionChanged="CameraInstances_OnSelectionChanged" DisplayMemberPath="Name"
IsSynchronizedWithCurrentItem="True"
Height="30"/>
此外,我还有另一个ComboBox
用作过滤器,在SelectionChanged
事件中我清除可观察的集合并根据选定的过滤器广告新项目:
public void Load(ObservableCollection<Object> objects, CameraBehavior behavior)
{
try
{
objects.Clear();
if (behavior == CameraBehavior.Undefined)
{
if (firstPersonCameraContainer.Count > 0)
{
foreach (KeyValuePair<string, FirstPersonCamera> camera in firstPersonCameraContainer)
{
objects.Add(camera);
}
}
if (thirdPersonCameraContainer.Count > 0)
{
foreach (KeyValuePair<string, ThirdPersonCamera> camera in thirdPersonCameraContainer)
{
objects.Add(camera);
}
}
if (flightCameraContainer.Count > 0)
{
foreach (KeyValuePair<string, FlightCamera> camera in flightCameraContainer)
{
objects.Add(camera);
}
}
}
else if (behavior == CameraBehavior.FirstPerson)
{
if (firstPersonCameraContainer.Count > 0)
{
foreach (KeyValuePair<string, FirstPersonCamera> camera in firstPersonCameraContainer)
{
objects.Add(camera);
}
}
}
else if (behavior == CameraBehavior.ThirdPerson)
{
if (thirdPersonCameraContainer.Count > 0)
{
foreach (KeyValuePair<string, ThirdPersonCamera> camera in thirdPersonCameraContainer)
{
objects.Add(camera);
}
}
}
else
if (behavior == CameraBehavior.Flight)
{
if (flightCameraContainer.Count > 0)
{
foreach (KeyValuePair<string, FlightCamera> camera in flightCameraContainer)
{
objects.Add(camera);
}
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message + e.StackTrace + e.TargetSite);
}
}
}
但实际上在 collection 更新后ComboBox
似乎ComboBox
失去了ItemsSource
. 有人知道为什么吗?