Binded ComboBox在清除后丢失项目名称并将对象添加到集合中

时间:2013-06-26 09:42:05

标签: wpf xaml data-binding

我在C#代码中有一个observableCollection属性,并且在此可观察集合中绑定了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);
         }
     }
}

但实际上,在ComboBox中更新了收集后,似乎ComboBox丢失了ItemsSource。有人知道为什么吗?

1 个答案:

答案 0 :(得分:0)

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