尝试绑定到 C# class 字段时出现 BindingExpression 路径错误
BindingExpression path error when attempting to bind to a C# class field
我有一个相当简单的绑定(使用 {Binding}
,而不是 {x:Bind}
),但它不起作用:
<ListView
ItemsSource="{x:Bind ViewModel.Items, Mode=OneWay}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:Item">
<StackPanel>
<TextBlock Text="{Binding MyField}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
编译成功,但失败并显示错误消息:
Error: BindingExpression path error: 'MyField' property not found on 'MyNamespace.Item'. BindingExpression: Path='MyField' DataItem='MyNamespace.Item'; target element is 'Windows.UI.Xaml.Controls.TextBlock' (Name='null'); target property is 'Text' (type 'String')
相同的绑定适用于 x:Bind
:
<TextBlock Text="{x:Bind MyField}" />
ViewModel 和 Item 定义
namespace MyNamespace
{
public class Item : INotifyPropertyChanged
{
public Item()
{
// ...
}
public readonly string MyField = "Foo";
// ...
}
public class MainPageViewModel : INotifyPropertyChanged
{
public MainPageViewModel()
{
// ...
}
private ItemsCollection _itemsCollection = new ItemsCollection();
public ObservableCollection<Item> Items
{
get { return _itemsCollection.Items; }
}
// ...
}
}
MyField
是 C# class 字段,不能与 {Binding}
.
绑定
为避免这种情况,请将 MyField
更改为 属性:
public class Item : INotifyPropertyChanged
{
// ...
private readonly string m_myField = "Foo";
public string MyField { get => m_myField; }
}
那么{Binding MyField}
就可以正常工作了。
见this Whosebug question about the difference between C# fields and properties:
4. Only Properties can be used in Binding Source
Binding Source helps us to decrease the number of lines of code. Fields are not accepted by BindingSource. We should use Properties for that.
这似乎类似于 WPF(也 can't bind on fields)。
我有一个相当简单的绑定(使用 {Binding}
,而不是 {x:Bind}
),但它不起作用:
<ListView
ItemsSource="{x:Bind ViewModel.Items, Mode=OneWay}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:Item">
<StackPanel>
<TextBlock Text="{Binding MyField}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
编译成功,但失败并显示错误消息:
Error: BindingExpression path error: 'MyField' property not found on 'MyNamespace.Item'. BindingExpression: Path='MyField' DataItem='MyNamespace.Item'; target element is 'Windows.UI.Xaml.Controls.TextBlock' (Name='null'); target property is 'Text' (type 'String')
相同的绑定适用于 x:Bind
:
<TextBlock Text="{x:Bind MyField}" />
ViewModel 和 Item 定义
namespace MyNamespace
{
public class Item : INotifyPropertyChanged
{
public Item()
{
// ...
}
public readonly string MyField = "Foo";
// ...
}
public class MainPageViewModel : INotifyPropertyChanged
{
public MainPageViewModel()
{
// ...
}
private ItemsCollection _itemsCollection = new ItemsCollection();
public ObservableCollection<Item> Items
{
get { return _itemsCollection.Items; }
}
// ...
}
}
MyField
是 C# class 字段,不能与 {Binding}
.
为避免这种情况,请将 MyField
更改为 属性:
public class Item : INotifyPropertyChanged
{
// ...
private readonly string m_myField = "Foo";
public string MyField { get => m_myField; }
}
那么{Binding MyField}
就可以正常工作了。
见this Whosebug question about the difference between C# fields and properties:
4. Only Properties can be used in Binding Source
Binding Source helps us to decrease the number of lines of code. Fields are not accepted by BindingSource. We should use Properties for that.
这似乎类似于 WPF(也 can't bind on fields)。