如何在StackLayout中更改颜色?

时间:2017-07-16 23:25:02

标签: c# android xaml

我使用Visual Studio将应用程序开发到Table 1 ColumnA ColumnB Account1 DeptA Account2 DeptA Account3 DeptA Account4 DeptB Account5 DeptB Table 2 ColumnC ColumnA ColumnD ColumnE(Date) Deposit1 Account1 10 Deposit1 Account2 20 Deposit1 Account3 10 Deposit2 Account1 10 Deposit2 Account2 30 Deposit2 Account3 30 Deposit3 Account2 20 Deposit3 Account3 10 Deposit1 Account4 20 Deposit1 Account5 20 Deposit2 Account4 10 Deposit2 Account5 20 Expected Output Count Mode(ColumnD) ColumnC ColumnB 2 10 Deposit1 Dept1 2 30 Deposit2 Dept1 1 10 Deposit3 Dept1 2 20 Deposit1 Dept2 1 10 Deposit2 Dept2 ,并且我使用以下代码存档Android

ItemsPage.xaml

在我的<ContentPage.Content> <StackLayout> <ListView x:Name="ItemsListView" ItemsSource="{Binding Items}" VerticalOptions="FillAndExpand" HasUnevenRows="true" RefreshCommand="{Binding LoadItemsCommand}" IsPullToRefreshEnabled="true" IsRefreshing="{Binding IsBusy, Mode=OneWay}" CachingStrategy="RecycleElement" ItemSelected="OnItemSelected"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <StackLayout x:Name="General" Orientation="Horizontal" HorizontalOptions="Fill" Padding="5"> <Image Source="{Binding FileName, Converter={StaticResource ImageConverter}}" HeightRequest="150" WidthRequest="150" AbsoluteLayout.LayoutBounds="250.25, 0.25, 50, 50 "/> <StackLayout Orientation="Vertical"> <Label Text="ST:" /><Label Text = "{Binding ST_string}" FontSize="24" AbsoluteLayout.LayoutBounds="0.25, 0.25, 400, 40"/> <Label Text="Folio:" /><Label Text = "{Binding Folio_string}" FontSize="24" AbsoluteLayout.LayoutBounds="0.25, 0.25, 400, 40"/> <Label Text="txt" /><Label Text = "{Binding Sent} " FontSize="24" AbsoluteLayout.LayoutBounds="0.25, 0.25, 400, 40"/> </StackLayout> </StackLayout> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView> </StackLayout> </ContentPage.Content> 我无法使用ItemsPage.xaml.cs访问我的StackLayout,我需要用颜色绘制它,但我不能,请帮忙。

一般情况下我不能做General.Background,我不知道访问它。

谢谢!

1 个答案:

答案 0 :(得分:1)

DataTemplate以外的元素无法从DataTemplate外部访问;这包括代码隐藏(xaml.cs文件)。

DataTemplates以特殊方式处理。它们被用作ListView内每个项目的模板(因此名称)。这意味着在运行时,每个项目的DataTemplate内的内容都将成为实例。如果该列表中有20个项目,那么名称为StackLayout的将为General个20 DataTemplates。您可以在docs中了解StackLayout

如果您想设置StackLayout的背景颜色,最简单的方法是直接在<StackLayout x:Name="General" BackgroundColor="Blue" Orientation="Horizontal"... 元素上执行此操作:

ContentView

或者,您可以创建ViewCell并将其放在BindingContext内。 ContentView的{​​{1}}将自动设置为当前项。 ContentView有点像ContentPage s,但您可以像其他任何View一样在页面内使用它们(例如ButtonBoxView。< / p>

修改

要添加ContentView右键单击并添加新文件,请选择 ContentView 。将XAML放在ViewCell内的ContentView内:

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Testing.MyView">
    <ContentView.Content>
        <StackLayout x:Name="General" Orientation="Horizontal" HorizontalOptions="Fill" Padding="5">
            <Image Source="{Binding FileName, Converter={StaticResource ImageConverter}}" HeightRequest="150" WidthRequest="150" AbsoluteLayout.LayoutBounds="250.25, 0.25, 50, 50 " />
            <StackLayout Orientation="Vertical">
                <Label Text="ST:" />
                <Label Text="{Binding ST_string}" FontSize="24" AbsoluteLayout.LayoutBounds="0.25, 0.25, 400, 40" />
                <Label Text="Folio:" />
                <Label Text="{Binding Folio_string}" FontSize="24" AbsoluteLayout.LayoutBounds="0.25, 0.25, 400, 40" />
                <Label Text="txt" />
                <Label Text="{Binding Sent} " FontSize="24" AbsoluteLayout.LayoutBounds="0.25, 0.25, 400, 40" />
            </StackLayout>
        </StackLayout>
    </ContentView.Content>
</ContentView>

在代码隐藏中,您可以访问所有控件:

public partial class MyView : ContentView
{
    public MyView()
    {
        InitializeComponent();

        General.BackgroundColor = true ? Color.Blue : Color.Brown;
    }
}

然后将ContentView添加到ViewCell

<ListView x:Name="ItemsListView" ItemsSource="{Binding Items}" VerticalOptions="FillAndExpand" HasUnevenRows="true" RefreshCommand="{Binding LoadItemsCommand}" IsPullToRefreshEnabled="true" IsRefreshing="{Binding IsBusy, Mode=OneWay}" CachingStrategy="RecycleElement" ItemSelected="OnItemSelected">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <local:MyView/>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>