Wpf 显示的组合框项目未更新

Wpf 显示的组合框项目未更新,wpf,combobox,refresh,Wpf,Combobox,Refresh,我对组合框中的项目列表有问题。当(从文件)重新加载itemsource时,它们不会更新 WPF如下所示: <DockPanel x:Name="Dock_Profil" DataContext="{Binding Profile, UpdateSourceTrigger=PropertyChanged,NotifyOnSourceUpdated= True}"> <StackPanel DockPanel.Dock="Top" Orien

我对组合框中的项目列表有问题。当(从文件)重新加载itemsource时,它们不会更新

WPF如下所示:

<DockPanel x:Name="Dock_Profil" DataContext="{Binding Profile, UpdateSourceTrigger=PropertyChanged,NotifyOnSourceUpdated= True}">
                    <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
                        <ComboBox Margin="5" Width="200" ItemsSource="{Binding}"  DisplayMemberPath="ProfilName" IsSynchronizedWithCurrentItem="True"
                                  x:Name="cmbProfil" SelectedIndex="0" ></ComboBox>
                        <Button Margin="5">
                            <StackPanel Orientation="Horizontal" >
                                <Image Margin="2" Stretch="None" Source="/MEC_EDINeu;component/Resources/Add24.png" />
                                <Label>Neues Profil</Label>
                            </StackPanel>
                        </Button>
                    </StackPanel>
在某些时候,我需要重新加载配置文件的内容,以便

Profile.Load()
OnPropertyChanged("Profile")
被称为。(OnPropertyChanged在ViewModelBase.vb中实现,并传递到MainWindowViewModel)

当我随后在MainWindow.xaml.vb中使用以下命令进行检查时:

For Each item As EDIProfil In cmbProfil.Items
            MsgBox(item.ProfilName & "__" & item.lastFA)
        Next
正确的项目在那里

但是GUI中的组合框仍然显示旧内容


我找到了一个解决方法(但我不想对所有组合框都使用): 如果我(在mainwindow.xaml.vb中)使用该行:

cmbProfil.Items.Refresh()
组合框显示的项目的更新工作(但不应该绑定到它?)


我是WPF的新手,希望能在这里得到一些帮助

提前谢谢


当我在MainWindowViewModel中加载数据时(这是正确的方法吗?)


它能用

试试这个。。。删除DataContext绑定并将ItemsSource直接绑定到
配置文件

   <DockPanel x:Name="Dock_Profil">
       <StackPanel DockPanel.Dock="Top"
                   Orientation="Horizontal">
           <ComboBox Margin="5"
                     Width="200"
                     ItemsSource="{Binding Profile,  
                                           UpdateSourceTrigger=PropertyChanged,
                                           NotifyOnSourceUpdated= True}"   ... />


谢谢,但是已经尝试过了(并且没有改变行为),datacontext也用于dockpanel中的其他元素。如果我在重新加载项目之前没有打开组合框,它们显示正确。您是否使用任何类型的多线程更新组合框?您的自定义
EDIProfile
类是否覆盖任何基本
observedcollection
方法?@Rachel无覆盖函数。但我在课堂上有一个加载函数。在我将加载移到mainwindowviewmodel之后,它似乎可以工作。通过最新添加(请参阅原始文章最后一次编辑),它甚至可以在没有onpropertychanged(“概要”)的情况下工作,因为我猜observable集合可以完成这项工作。在类中对数据所做的更改不会传递给视图。
Public Sub loadProfile()
        'Profile.Load()
        Profile.Clear()
        Dim xmls As XmlSerializer = New XmlSerializer(GetType(EDIProfile))
        Dim reader As New StreamReader(System.Reflection.Assembly.GetExecutingAssembly().Location.Substring(0, System.Reflection.Assembly.GetExecutingAssembly().Location.LastIndexOf("\") + 1) & "EDIProfile.xml")
        Dim temp As New EDIProfile
        '  MsgBox("KK")
        temp = xmls.Deserialize(reader)
        For Each item As EDIProfil In temp
            Profile.Add(item)
        Next
        reader.Close()
        OnPropertyChanged("Profile")

    End Sub
   <DockPanel x:Name="Dock_Profil">
       <StackPanel DockPanel.Dock="Top"
                   Orientation="Horizontal">
           <ComboBox Margin="5"
                     Width="200"
                     ItemsSource="{Binding Profile,  
                                           UpdateSourceTrigger=PropertyChanged,
                                           NotifyOnSourceUpdated= True}"   ... />