集合中的绑定项目工作正常,但是当我尝试绑定变量时Count不起作用,有人可以帮我吗?
<Label Text="{Binding Count}" TextColor="#4F4F4F" FontSize="10" FontAttributes="Bold"/>
我做了一个测试,将Label移出ListView并且它工作得很好,但我需要它在Listview中工作。
视图模型:
class ListViewModel : INotifyPropertyChanged
{
public ObservableCollection<Item> Items { get; }
public ListViewModel()
{
Items = new ObservableCollection<Item>(new[]
{
new Item { Text = "Test 1", Detail = "Detail 1", Foto = "computer2.png" },
new Item { Text = "Test 2", Detail = "Detail 2", Foto = "computer4.png" },
});
private int count = 1;
public int Count
{
get { return count; }
set
{
count = value;
OnPropertyChanged();
}
}
XAML:
<ListView
ItemsSource="{Binding Items}"
<ListView.Header>
<StackLayout>
<Label Text="{Binding Count}"
TextColor="#4F4F4F" FontSize="10"
FontAttributes="Bold"/>
</StackLayout>
</ListView.Header>
</ListView>
答案 0 :(得分:1)
尝试以下内容。此外,我假设您发布的内容不是您的完整代码,因为我们无法看到您的OnPropertyChanged实现或您如何设置计数。
<ContentPage
//BlahBlah Headings
x:Name="PageName">
<ListView ItemsSource="{Binding Items}">
<ListView.Header>
<StackLayout>
<Label Text="{Binding Source={x:Reference PageName}, Path=BindingContext.Count}"
TextColor="#4F4F4F" FontSize="10"
FontAttributes="Bold"/>
</StackLayout>
</ListView.Header>
</ListView>
</ContentPage>
答案 1 :(得分:0)
我已经完成了这个小回购,它没有特殊的绑定工作
ViewModel + Model
namespace TestRelativeLayout.ViewModels
{
[ImplementPropertyChanged]
public class MyPage4ViewModel
{
public ObservableCollection<Item> Items { get; set; }
public int Counter { get; set; } = 111;
public string MyCounterFooter { get; set; } = "888";
public MyPage4ViewModel() {
Items = new ObservableCollection<Item>(new[]
{
new Item { Text = "Test 1", Detail = "Detail 1", Foto = "computer2.png" },
new Item { Text = "Test 2", Detail = "Detail 2", Foto = "computer4.png" },
});
}
public class Item
{
public string Detail { get; set; }
public string Foto { get; set; }
public string Text { get; set; }
}
}
}
的Xaml
namespace TestRelativeLayout.ViewModels
{
[ImplementPropertyChanged]
public class MyPage4ViewModel
{
public ObservableCollection<Item> Items { get; set; }
public int Counter { get; set; } = 111;
public string MyCounterFooter { get; set; } = "888";
public MyPage4ViewModel() {
Items = new ObservableCollection<Item>(new[]
{
new Item { Text = "Test 1", Detail = "Detail 1", Foto = "computer2.png" },
new Item { Text = "Test 2", Detail = "Detail 2", Foto = "computer4.png" },
});
}
public class Item
{
public string Detail { get; set; }
public string Foto { get; set; }
public string Text { get; set; }
}
}
}