本文介绍了如何通过mvvm在viewmodel中显示selecteditem从列表视图到xamarin中的条目的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在通过xamarin中的SQLite进行粗操作,但是我想显示从列表视图到条目的数据,以便我可以更新它,但是我不知道如何调用绑定在xaml中的选定项
i am doing crud operation through SQLite in xamarin but i want to show data from listview to entry so that i can update it but i don't know how to call selected item that is binded in xaml
<ListView ItemsSource="{Binding companylist}" SelectedItem="{Binding selectedname}" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding name}"/>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<StackLayout>
<Entry Text="{Binding id}" IsVisible="False"/>
<Entry Placeholder="Name" Text="{Binding name}"/>
<Button Text="Update" Command="{Binding UpdateCompanyCommand}"/>
</StackLayout>
</StackLayout>
视图模型中的代码是
public Command UpdateCompanyCommand { get; }
async Task UpdateCompany()
{
var db = new SQLiteConnection(dbpath);
Company company = new Company()
{
id=id,
name = Name
};
db.Update(company);
await Xamarin.Forms.Application.Current.MainPage.DisplayAlert("Message", "Name is Updated", "Ok");
}
推荐答案
您可以通过将您的name属性绑定到带有ListView的SelectedItem的Entry中来实现,如下所示,
You can achieve it through binding your name property into Entry with SelectedItem of ListView like below,
<Entry Text="{Binding name}"
BindingContext="{Binding SelectedItem, Source={x:Reference listView}}" />
为ListView输入一个名称,并通过所选项目设置Entry的BindingContext.每当列表视图所选项目更改时,条目就会更新.
Put a name for ListView and set BindingContext of Entry by your selected item. The entry will be updated whenever the listview selected item changed.
这篇关于如何通过mvvm在viewmodel中显示selecteditem从列表视图到xamarin中的条目的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!