[英]How to change color in StackLayout?
I developing app with Visual Studio to Android
, and I have file ItemsPage.xaml
with this code : 我使用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>
And in my ItemsPage.xaml.cs
I can't access to my StackLayout
with Name="General"
, I need paint this with color, but I can't, please help. 在我的
ItemsPage.xaml.cs
我无法使用Name="General"
访问我的StackLayout
,我需要用颜色绘制它,但是我不能,请帮忙。
In general I can't do General.Background, I don't know access this. 通常,我不能执行General.Background,我不知道访问此方法。
Thanks! 谢谢!
Elements inside a DataTemplate
are not accessible from outside the DataTemplate
; 无法从
DataTemplate
外部访问DataTemplate
内部的元素; this includes the code-behind (the xaml.cs
file). 这包括后台代码(
xaml.cs
文件)。
DataTemplates
are handled in a special way. DataTemplates
以特殊方式处理。 They're used as a template (hence the name) for each item inside the ListView
. 它们用作
ListView
内每个项目的模板(因此而得名)。 This means that at runtime there's going to be an instance of the contents inside the DataTemplate
for each item. 这意味着在运行时,每个项目的
DataTemplate
内将有一个内容实例。 If you have 20 items in that list, there's going to be 20 StackLayout
s with the name General
. 如果该列表中有20个项目,那么将有20个
StackLayout
,名称为General
。 You can read about DataTemplates
in the docs . 您可以在docs中了解有关
DataTemplates
的信息。
If you want to set the background color of the StackLayout
, the easiest way is to do that directly on the StackLayout
element: 如果要设置
StackLayout
的背景色,最简单的方法是直接在StackLayout
元素上进行StackLayout
:
<StackLayout x:Name="General" BackgroundColor="Blue" Orientation="Horizontal"...
Alternatively you can create a ContentView
and put that inside the ViewCell
. 或者,您可以创建一个
ContentView
并将其放在ViewCell
。 The BindingContext
of the ContentView
will automatically be set to the current item. ContentView
的BindingContext
将自动设置为当前项目。 ContentView
s are a bit like ContentPage
s, but you can use them inside a page like any other View
(like a Button
or a BoxView
. ContentView
有点像ContentPage
,但是您可以像其他任何View
一样在页面内使用它们(例如Button
或BoxView
。
Edit 编辑
To add a ContentView
right-click and add a new file, choose ContentView . 要右键单击添加
ContentView
并添加新文件,请选择ContentView 。 Put the XAML inside ViewCell
inside the ContentView
: 将XAML放在
ContentView
内的ViewCell
内:
<?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>
In the code-behind, you can access all controls: 在后面的代码中,您可以访问所有控件:
public partial class MyView : ContentView
{
public MyView()
{
InitializeComponent();
General.BackgroundColor = true ? Color.Blue : Color.Brown;
}
}
Then add the ContentView
to the ViewCell
: 然后将
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.