UWP:无效的绑定路径:属性 在 DataTemplate 类型上找不到
UWP: Invalid Binding Path : Property not found on type DataTemplate
我正在构建我的第一个 UWP 应用程序,我使用 WPF 有一段时间了,
我正在尝试绑定 Listview itemsSource 但我不断收到下一个错误:
- 无效的绑定路径 'Fletes':属性 'Fletes' 在类型 'DataTemplate' 上找不到。
我试过了
<ListView ItemsSource ={Binding Viewmodel.Fletes}/>
在 xaml 中,移除 x:DataType
和
在 .cs
上使用 this.DataContext = ViewModel
这是我的代码:
Views/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));
}
}
}
Invalid binding path 'Fletes' : Property 'Fletes' not found on type 'DataTemplate'.
在 DataTemplate 中,Path 的值不是在页面的上下文中解释的,而是在被模板化的数据对象的上下文中解释的。 DataTemplate里面的ListView能绑定的数据取决于MasterDetailsView的ItemsSource,你没有为MasterDetailsView设置ItemsSource,ListView无法正确绑定。
此外,MasterDetailsView 控件以 master/details 模式显示项目。它显示了“主面板”中的项目集合,因此您无需在其中声明 ListView,“主面板”作为 ListView,您可以直接为 MasterDetailsView 设置 ItemsSource。例如:
HomeFletes.xaml
<controls:MasterDetailsView x:Name="MyDetailsView"
Loaded="MasterDetailsView_Loaded"
CompactModeThresholdWidth="720" ItemsSource="{x:Bind ViewModel.Fletes}">
<controls:MasterDetailsView.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>
</controls:MasterDetailsView.ItemTemplate>
</controls:MasterDetailsView>
我正在构建我的第一个 UWP 应用程序,我使用 WPF 有一段时间了, 我正在尝试绑定 Listview itemsSource 但我不断收到下一个错误:
- 无效的绑定路径 'Fletes':属性 'Fletes' 在类型 'DataTemplate' 上找不到。
我试过了
<ListView ItemsSource ={Binding Viewmodel.Fletes}/>
在 xaml 中,移除 x:DataType
和 在 .cs
上使用this.DataContext = ViewModel
这是我的代码:
Views/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));
}
}
}
Invalid binding path 'Fletes' : Property 'Fletes' not found on type 'DataTemplate'.
在 DataTemplate 中,Path 的值不是在页面的上下文中解释的,而是在被模板化的数据对象的上下文中解释的。 DataTemplate里面的ListView能绑定的数据取决于MasterDetailsView的ItemsSource,你没有为MasterDetailsView设置ItemsSource,ListView无法正确绑定。
此外,MasterDetailsView 控件以 master/details 模式显示项目。它显示了“主面板”中的项目集合,因此您无需在其中声明 ListView,“主面板”作为 ListView,您可以直接为 MasterDetailsView 设置 ItemsSource。例如:
HomeFletes.xaml
<controls:MasterDetailsView x:Name="MyDetailsView"
Loaded="MasterDetailsView_Loaded"
CompactModeThresholdWidth="720" ItemsSource="{x:Bind ViewModel.Fletes}">
<controls:MasterDetailsView.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>
</controls:MasterDetailsView.ItemTemplate>
</controls:MasterDetailsView>