2017-07-16 95 views
0

我開發與Visual Studio應用Android,我有文件ItemsPage.xaml與此代碼:如何更改StackLayout中的顏色?

<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我無法訪問我的StackLayoutName="General",我需要色漆這一點,但我不能,請幫助。

一般我不能做General.Background,我不知道這個訪問。

謝謝!

回答

1

DataTemplate中的元素不能從DataTemplate之外訪問;這包括代碼隱藏(xaml.cs文件)。

DataTemplates以特殊方式處理。它們被用作ListView中每個項目的模板(因此名稱)。這意味着在運行時將會爲每個項目的DataTemplate中的內容的一個實例。如果您在該列表中有20個項目,將會有名稱爲General的20 StackLayout。您可以在docs中閱讀DataTemplates

如果你想設置的StackLayout的背景色,最簡單的方法是做直接的StackLayout元素:

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

或者,你可以創建一個ContentView,把裏面的ViewCellContentViewBindingContext將自動設置爲當前項目。 ContentView s爲有點像ContentPage S,但是你可以使用它們的網頁裏像任何其他View(如ButtonBoxView

編輯

添加ContentView右鍵單擊並添加新的文件,選擇內容查看把XAML內ViewCellContentView

<?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> 

在代碼隱藏,你可以訪問所有的控件:或者

<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> 
+0

但我需要修改BACKGROUNDCOLOR在某些情況下,你應該解釋一下我的解決方案爲:

public partial class MyView : ContentView { public MyView() { InitializeComponent(); General.BackgroundColor = true ? Color.Blue : Color.Brown; } } 

然後添加ContentViewViewCell這似乎是我需要的。 – fabulias

+0

您可以通過查看以下簡短教程開始使用:https://xamarinhelp.com/xamarin-forms-user-control/實際上,「ContentView」就像是「ContentPage」,但您可以將「ContentView」 '在頁面內 –

+0

我不明白兄弟,你能幫我嗎?請 – fabulias