Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

One page two binding

$
0
0

In one page I need two Binding, the first is a listView, it works.
Now I need to add the second, but there is only one this.BindingContext I don't know how to set the second binding.

The exists code:
MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:ChannelChat"
             x:Class="ChannelChat.MainPage"
             Title="频道聊天信息">

    <StackLayout>

        <StackLayout Orientation="Horizontal" Spacing="10">
            <Label Text="123456" FontSize="Medium"/>
            <Label Text="我的聊天昵称" VerticalOptions="Center" HorizontalOptions="FillAndExpand" FontSize="Medium"/>
            <Button Text="频道" Clicked="Button_Clicked"/>
        </StackLayout>

        <ListView x:Name="listView" ItemsSource="{Binding}" HasUnevenRows="True">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Padding="0" Spacing="0" Orientation="Vertical">
                            <StackLayout Padding="0" Spacing="10" Orientation="Horizontal">
                            <Label Text="{Binding Number, StringFormat='{0:000}'}" FontSize="Micro" TextColor="Silver" />
                            <Label Text="{Binding Nickname}" FontSize="Micro" TextColor="Silver" />
                            <Label Text="{Binding Ip}" FontSize="Micro" TextColor="Silver" />
                            </StackLayout>
                        <Label Text="{Binding Message}" FontSize="Medium" TextColor="{Binding TextColor}" Margin="3"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

        <StackLayout Orientation="Horizontal">
            <Entry Placeholder="输入聊天信息" VerticalOptions="Center" HorizontalOptions="FillAndExpand"
                   x:Name="sendText" ReturnType="Send" Completed="SendButton_Clicked"/>
            <Button Text="发送"
                   x:Name="sendButton" Clicked="SendButton_Clicked"/>
        </StackLayout>

    </StackLayout>

</ContentPage>

MainPage.xaml.cs

    public partial class MainPage : ContentPage
    {
        public ObservableCollection<TextMessage> textMessages;

        public MainPage()
        {
            InitializeComponent();

            textMessages = new ObservableCollection<TextMessage>();
            this.BindingContext = textMessages;
        }
    }

Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>