0

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. 有人知道为什么吗?

4

1 回答 1

0

抱歉,问题已由我自己解决。这实际上是因为我向集合中添加了错误的类型(字典而不是具体类型)。

于 2013-06-26T09:57:54.700 回答