C# WPF:在GridView模式下将ListView与集合绑定。错误
因此,我正在尝试将GridView模式下的ListView与我的集合绑定。 以下是XAML:C# WPF:在GridView模式下将ListView与集合绑定。错误,c#,wpf,listview,gridview,C#,Wpf,Listview,Gridview,因此,我正在尝试将GridView模式下的ListView与我的集合绑定。 以下是XAML: <Window x:Class="MyApp.ParametersWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" mc:Ignorable="d" xmlns:d="http
<Window x:Class="MyApp.ParametersWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="340" Width="300"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
<Button Content="Cancel" Height="23" HorizontalAlignment="Left" Margin="39,254,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
<Button Content="Run Test" Height="23" HorizontalAlignment="Right" Margin="0,254,51,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
<ListView Height="227" HorizontalAlignment="Left" Margin="21,12,0,0" Name="listView1" VerticalAlignment="Top" Width="237" ItemsSource="{Binding FileNames}">
<GridView>
<GridViewColumn Width="120" Header="Vorname" DisplayMemberBinding="{Binding Name}" /> **<!--Error in this line-->**
</GridView>
</ListView>
</Grid>
****
以下是*.cs:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
namespace AtmlServiceClient
{
public partial class ParametersWindow : Window
{
public class FileInfo
{
public string Name { get; set; }
public DateTime LastModified { get; set; }
}
ObservableCollection<FileInfo> mFileNames;
public ObservableCollection<FileInfo> FileNames
{
get
{
return mFileNames;
}
}
public ParametersWindow()
{
mFileNames = new ObservableCollection<FileInfo>();
InitializeComponent();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
FileNames.Add(new FileInfo() {Name = "X", LastModified = DateTime.Now});
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用System.Linq;
使用System.Windows;
命名空间AtmlServiceClient
{
公共部分类参数swindow:窗口
{
公共类文件信息
{
公共字符串名称{get;set;}
公共日期时间LastModified{get;set;}
}
可观察的集合文件名;
公共可观测集合文件名
{
得到
{
返回文件名;
}
}
公共参数swindow()
{
mFileNames=新的ObservableCollection();
初始化组件();
}
私有无效按钮2\u单击(对象发送者,路由目标)
{
添加(newfileinfo(){Name=“X”,LastModified=DateTime.Now});
}
}
}
当窗口出现时,我收到下一个错误。(按钮单击时不可用)
错误:
PresentationFramework.dll中发生了类型为“System.Windows.Markup.XamlParseException”的首次异常。其他信息:“向类型为“System.Windows.Controls.ItemCollection”的集合添加值”引发了异常。“行号“11”和行位置“62”
请帮我解决它。我认为您的错误在GridView中的XAML中 试试这个:
不同之处在于
<ListView> <GridView>
你会有
<ListView> <ListView.View> <GridView>
@user2706838它对您有帮助吗?