【发布时间】:2020-11-08 17:47:55
【问题描述】:
我正在构建我的第一个 UWP 应用程序,我使用 WPF 已经有一段时间了, 我正在尝试绑定 Listview itemsSource,但我不断收到下一个错误:
- 绑定路径“Fletes”无效:在类型“DataTemplate”上找不到属性“Fletes”。
我试过了
<ListView ItemsSource ={Binding Viewmodel.Fletes}/>
在 xaml 中,删除 x:DataType
和
在 .cs 上使用 this.DataContext = ViewModel
这是我的代码:
视图/HomeFletes.xaml.cs
namespace Internal.Views
{
public sealed partial class HomeFletes : Page
{
public FletesViewModel ViewModel { get; set; }
public HomeFletes()
{
this.InitializeComponent();
this.ViewModel = new FletesViewModel();
}
private void MasterDetailsView_Loaded(object sender, RoutedEventArgs e)
{
Console.Write("loaded");
}
}
}
Views/HomeFletes.xaml
<Page
x:Class="Internal.Views.HomeFletes"
xmlns:local="using:Internal"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:model="using:Internal.Models"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<controls:MasterDetailsView
Loaded="MasterDetailsView_Loaded"
CompactModeThresholdWidth="720">
<controls:MasterDetailsView.ItemTemplate>
<DataTemplate>
<ListView ItemsSource="{x:Bind ViewModel.Fletes}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="model:Flete">
<RelativePanel Margin="24">
<StackPanel Height="100" Width="100" Background="OrangeRed">
<TextBlock Text="{x:Bind Path=Transporte}"/>
<TextBlock Text="{x:Bind Path=Comentario}" x:Phase="2"/>
</StackPanel>
</RelativePanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</DataTemplate>
</controls:MasterDetailsView.ItemTemplate>
</controls:MasterDetailsView>
</Page>
/ViewModels/FletesViewModel.cs
namespace Internal.ViewModels
{
public class FletesViewModel : BindableBase
{
private ObservableCollection<Flete> fletes = new ObservableCollection<Flete>();
public ObservableCollection<Flete> Fletes { get { return this.fletes; } }
public FletesViewModel()
{
this.fletes.Add(new Flete() { Transporte = "Transporte #1", Fecha = DateTime.Now, Comentario = "Este es el comentario #1" });
this.fletes.Add(new Flete() { Transporte = "Transporte #2", Fecha = DateTime.Now, Comentario = "Este es el comentario #2" });
this.fletes.Add(new Flete() { Transporte = "Transporte #3", Fecha = DateTime.Now, Comentario = "Este es el comentario #3" });
this.fletes.Add(new Flete() { Transporte = "Transporte #4", Fecha = DateTime.Now, Comentario = "Este es el comentario #4" });
}
}
}
/Models/Flete.cs
namespace Internal.Models
{
public class Flete : INotifyPropertyChanged
{
public string Transporte { get; set; }
public DateTime Fecha { get; set; }
public string Comentario { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
【问题讨论】:
-
xmlns:model="using:Internal.Models" 在这里寻找你的模型路径
-
但这是通往模型命名空间的正确路径。我迷路了。