如何在堆栈布局中更改颜色?



>我用Visual Studio开发应用程序来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我无法用Name="General"进入我的StackLayout,我需要用颜色涂上它,但我不能,请帮忙。

一般来说,我不能做一般背景,我不知道访问这个。

谢谢!

DataTemplate内的元素不能从DataTemplate外部访问;这包括代码隐藏(xaml.cs文件(。

DataTemplates以特殊方式处理。它们被用作ListView内每个项目的模板(因此得名(。这意味着在运行时,每个项目的DataTemplate内都会有一个内容的实例。如果该列表中有 20 个项目,则名称为General的 20 个StackLayout。您可以在文档中阅读有关DataTemplates的信息。

如果要设置StackLayout的背景颜色,最简单的方法是直接在StackLayout元素上执行此操作:

<StackLayout x:Name="General" BackgroundColor="Blue" Orientation="Horizontal"...

或者,您可以创建一个ContentView并将其放入ViewCell中。ContentViewBindingContext将自动设置为当前项目。ContentView有点像ContentPages,但您可以像任何其他View(如ButtonBoxView一样在页面中使用它们。

编辑

若要添加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>

最新更新