forms - In Xamarin form, is it possible to do a databinding outside a listview, like just in a label?
问题描述
Sorry for a bit confusing title.
I simply want to get my info from a db with a model. I know how I do it in a listview, but I cant seem to find information about doing it outside a listview. I know you can bind things to label, but then its just whats in my viewmodel.
Do I make any sense at all?
Thanks.
Update: I want to do this something like this listview
<ListView
BackgroundColor="Transparent"
CachingStrategy="RecycleElement"
HasUnevenRows="True"
IsPullToRefreshEnabled="True"
IsRefreshing="{Binding IsBusy, Mode=OneWay}"
ItemsSource="{Binding MyProfile}"
RefreshCommand="{Binding RefreshCommand}"
RefreshControlColor="Red"
SelectionMode="None"
SeparatorVisibility="None">
<ListView.ItemTemplate>
<DataTemplate x:DataType="model:MyProfile">
<ViewCell>
<Grid Padding="10">
<Frame CornerRadius="20" HasShadow="True">
<StackLayout Orientation="Horizontal">
<StackLayout VerticalOptions="Center">
<Label
FontSize="Large"
Text="{Binding Name}"
VerticalOptions="Center" />
<Label
FontSize="Large"
Text="{Binding id}"
VerticalOptions="Center" />
<Label
FontSize="Small"
Text="{Binding PhoneNumber}"
VerticalOptions="Center" />
</StackLayout>
</StackLayout>
</Frame>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
In a stacklayout with just labels:
<StackLayout BindableLayout.ItemsSource="{Binding MyProfile}"
Orientation="Horizontal">
<BindableLayout.ItemTemplate>
<DataTemplate x:DataType="model:MyProfile">
<controls:CircleImage Source="{Binding}"
Aspect="AspectFill"
WidthRequest="44"
HeightRequest="44"
... />
<Label Text="Enter profile"/>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
I just want to update a label and not a list with info from db using data binding.
解决方案
if you want to display data for a single object, you can do something like this
<Frame CornerRadius="20" HasShadow="True">
<StackLayout Orientation="Horizontal">
<StackLayout VerticalOptions="Center">
<Label
FontSize="Large"
Text="{Binding Name}"
VerticalOptions="Center" />
<Label
FontSize="Large"
Text="{Binding id}"
VerticalOptions="Center" />
<Label
FontSize="Small"
Text="{Binding PhoneNumber}"
VerticalOptions="Center" />
</StackLayout>
</StackLayout>
</Frame>
and in the code behind for your page
// where MyViewModel is a single instance of the model you want to display
BindingContext = MyViewModel;
推荐阅读
- vba - 将 NDR 邮件移动到不在收件箱中的文件夹
- spring - Spring数据中mongo聚合中使用Document.parse而不是Aggregation静态方法的问题
- html - 放置两个固定的 div 和下一个要滚动的内容
- reactjs - Google OAuth2 无法与 React Redux 一起使用
- javascript - 达到时间限制时禁用按钮
- c++ - 运行 Vroom 构建时 CodeBlocks 编译错误
- r - 如何对相似的行进行分组,并查询 R 中的某些行列?
- swagger-ui - OAS 3 中带有 ClientCrendentials 流的 oauth2 可以允许自定义标头吗?
- flutter - Flutter中根据Tab Bar名称显示Tab Bar查看产品
- sql - 如何从 SQL 中具有重复字符的列值(字符串)中获取值?