显示的组合框项目未更新

发布于 2024-12-04 18:06:55 字数 2480 浏览 1 评论 0原文

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

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>

属性配置文件是这种类型:

Public Class EDIProfile
    Inherits ObservableCollection(Of EDIProfil)

在某些时候,我需要重新加载配置文件的内容,因此

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 中加载数据时(无论如何这是正确的方法吗?):

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

它有效

I have a problem with the List of Items in a combobox. They dont update when the itemsource is reloaded (from a file)

The WPF looks like this:

<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>

The Property Profile is this type:

Public Class EDIProfile
    Inherits ObservableCollection(Of EDIProfil)

At certain points i need to reload the Content of Profile so

Profile.Load()
OnPropertyChanged("Profile")

is called. (OnPropertyChanged is implemented in the ViewModelBase.vb and passed on to MainWindowViewModel)

When i check afterwards in the MainWindow.xaml.vb using:

For Each item As EDIProfil In cmbProfil.Items
            MsgBox(item.ProfilName & "__" & item.lastFA)
        Next

the correct items are in there.

But the combobox in GUI still shows the old content.


A Workaround i found (but i dont want to use that for all comboboxes):
if i (in mainwindow.xaml.vb) use that line:

cmbProfil.Items.Refresh()

the update of the items displayed by the combobox work (but shouldnt the binding to that?)


I am pretty new to WPF, hoping to get some help here.

Thanks in advance


When I load the data in MainWindowViewModel (is that the correct way to do it anyway?):

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

It works

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏有森光若流苏 2024-12-11 18:06:55

试试这个...删除您的 DataContext 绑定并将 ItemsSource 直接绑定到 Profile

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

Try this ... remove your DataContext binding and bind ItemsSource directly to Profile.

   <DockPanel x:Name="Dock_Profil">
       <StackPanel DockPanel.Dock="Top"
                   Orientation="Horizontal">
           <ComboBox Margin="5"
                     Width="200"
                     ItemsSource="{Binding Profile,  
                                           UpdateSourceTrigger=PropertyChanged,
                                           NotifyOnSourceUpdated= True}"   ... />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文