首页 > 解决方案 > 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.

标签: formssqlitexamarinbinding

解决方案


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;

推荐阅读