gpt4 book ai didi

C# wpf 列表框未从 ObservableCollection 更新

转载 作者:行者123 更新时间:2023-12-03 10:55:01 24 4
gpt4 key购买 nike

我正在尝试获取使用 ListBox 所需的数据绑定(bind)。
我已将文本文件中的一些数据解析为 ObservableCollection<ViewModel>但列表框中的数据没有更新。

这里有一些信息:

从解析器写入的数据:

class MainData
{
private static ObservableCollection<GroupViewModel> groupModelList = new ObservableCollection<GroupViewModel>();
public static ObservableCollection<GroupViewModel> GroupModelList
{
get { return groupModelList; }
}
}

什么 GroupViewModel成立(不是一切,但都是一样的):
class GroupViewModel : INotifyPropertyChanged
{
private GroupModel groupModel;

public event PropertyChangedEventHandler PropertyChanged;

public GroupViewModel()
{
groupModel = new GroupModel();
}

public string Name
{
get { return groupModel.name; }
set
{
if (groupModel.name != value)
{
groupModel.name = value;
InvokePropertyChanged("Name");
}
}
}

...
}

还有什么 GroupModel持有:
class GroupModel
{
public string name { get; set; }
}

这就是解析器向 GroupModelView 添加新项目的方式。 :
if (split[0] == "group")
{
currentGroup = new GroupViewModel();
currentGroup.Name = split[1];

MainData.GroupModelList.Add(currentGroup);
}

我使用这些 XAML 选项在我的 WPF 应用程序中创建了一个 ListBox:
<Window x:Class="SoundManager.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:SoundManager.ViewModels"
xmlns:vm2="clr-namespace:SoundManager.Code"
Title="MainWindow" Height="720" Width="1280">
<Window.Resources>
<vm:MainViewModel x:Key="MainViewModel" />
<vm2:MainData x:Key="MainData" />
</Window.Resources>

<ListBox Grid.Row="2" Height="484" HorizontalAlignment="Left" Margin="12,0,0,0" Name="lbFoundItems" VerticalAlignment="Top" Width="201" ItemsSource="{Binding Source={StaticResource MainData}, Path=GroupModelList/Name}" />

但由于某种原因,数据未在 UI 中更新(新项目未在 UI 中明显添加)。
我刚刚开始使用 MVVM 模式和数据绑定(bind),但我不知道我做错了什么。

提前致谢!

最佳答案

GroupModelList/Name此处不是有效的属性路径。像这样设置它不会使 ListBox 显示 Name GroupModelList 中数据项的属性收藏。

您将不得不设置 ListBox 的 DisplayMemberPath属性(property):

<ListBox ItemsSource="{Binding Source={StaticResource MainData}, Path=GroupModelList}"
DisplayMemberPath="Name"/>

或设置 ItemTemplate属性(property):
<ListBox ItemsSource="{Binding Source={StaticResource MainData}, Path=GroupModelList}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

此外, GroupModelList属性不应该是静态的:
class MainData
{
private ObservableCollection<GroupViewModel> groupModelList =
new ObservableCollection<GroupViewModel>();

public ObservableCollection<GroupViewModel> GroupModelList
{
get { return groupModelList; }
}
}

那么你可能有 MainData作为 View 模型中的属性,并像这样绑定(bind) ListBox:
<ListBox ItemsSource="{Binding Source={StaticResource MainViewModel},
Path=MainData.GroupModelList}" .../>

关于C# wpf 列表框未从 ObservableCollection 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29510114/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com