C# 绑定到ListView上的ViewModel不会显示任何内容,但可以在其他控件上正常工作
我试图将ViewModel类中的ObservableCollection绑定到使用GridView显示行的ListView,但无法在列表中显示数据 出于测试目的,我放置了一个TextBlock,并且可以成功地通过绑定显示数据(但是,只有在代码隐藏中首先执行C# 绑定到ListView上的ViewModel不会显示任何内容,但可以在其他控件上正常工作,c#,wpf,mvvm,data-binding,C#,Wpf,Mvvm,Data Binding,我试图将ViewModel类中的ObservableCollection绑定到使用GridView显示行的ListView,但无法在列表中显示数据 出于测试目的,我放置了一个TextBlock,并且可以成功地通过绑定显示数据(但是,只有在代码隐藏中首先执行DataContext=ViewModel时,在XAML中执行ViewModel.MyData不起作用) 我注意到无论ItemsSource绑定到什么,列表中总是会显示随机数的空元素,我可以看出,当我将鼠标悬停在ListView上时,行会高亮显
DataContext=ViewModel
时,在XAML中执行ViewModel.MyData
不起作用)
我注意到无论ItemsSource
绑定到什么,列表中总是会显示随机数的空元素,我可以看出,当我将鼠标悬停在ListView上时,行会高亮显示。此数字与我的收藏容量不匹配
如果在代码隐藏中,我手动将ItemsSource
设置为要显示的集合,则会显示正确数量的元素,但仍然没有显示任何数据
XAML
<Page x:Class="GestionStockWPF.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:GestionStockWPF"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="MainPage"
ShowsNavigationUI="False">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="13*"/>
<RowDefinition Height="77*"/>
</Grid.RowDefinitions>
<!-- The Binding here seems to do nothing, there isn't the correct amount of rows in the app -->
<ListView x:Name="listView"
HorizontalAlignment="Stretch"
Grid.Row="1" Margin="30, 0, 30, 30"
ItemsSource="{Binding Source=Tests}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Path=A}"
Header="Asset Number" Width="200"/>
</GridView>
</ListView.View>
</ListView>
<!-- This textblock is just here for testing purposes, it successfully shows "A" -->
<TextBlock x:Name="textBlock" Text="{Binding Path=Tests[0].A}"
HorizontalAlignment="Left" Margin="252,28,0,0" TextWrapping="Wrap"
VerticalAlignment="Top"/>
</Grid>
</Page>
视图模型和测试类
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace GestionStockWPF
{
// Not sure if I have to implement INotifyPropertyChanged but my real class does it
public class Test : INotifyPropertyChanged
{
public string A { get { return "A"; } }
public string B { get { return "B"; } }
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
public class MainPageViewModel
{
public ObservableCollection<Test> Tests { get; set; }
public MainPageViewModel()
{
tests = new ObservableCollection<Test>();
for (int i = 0; i < 50; ++i)
{
Tests.Add(new Test());
}
}
}
}
使用System.Collections.ObjectModel;
使用系统组件模型;
使用System.Runtime.CompilerServices;
命名空间GestionStockWPF
{
//不确定是否必须实现INotifyPropertyChanged,但我的实际类实现了它
公共类测试:INotifyPropertyChanged
{
公共字符串A{get{返回“A”;}
公共字符串B{get{返回“B”;}
公共事件属性更改事件处理程序属性更改;
受保护的void OnPropertyChanged([CallerMemberName]字符串名称=null)
{
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(名称));
}
}
公共类MainPageViewModel
{
公共可观测集合测试{get;set;}
公共MainPageViewModel()
{
测试=新的可观察集合();
对于(int i=0;i<50;++i)
{
添加(新测试());
}
}
}
}
您的绑定不正确
应该是:
ItemsSource="{Binding tests}"
您需要指定绑定中的路径,而不是绑定源
显式路径
部分在绑定中是可选的,因此您还可以将文本块绑定更改为:
Text="{Binding tests[0].A}"
顺便说一下,属性应该使用
PascalCase
,因此最好将其称为TestsDataContext=this
不起作用,因为绑定需要属性,而ViewModel
是一个字段。从ItemsSource
中删除Source=
似乎可以解决显示的行数不正确的问题,这让我觉得Tests
现在已正确绑定到列表视图,但其中仍然没有内容。我还编辑了我原来的帖子,所以tests
变成了tests
Text="{Binding tests[0].A}"