簡體   English   中英

在DataTemplate Xamarin.Forms內綁定視圖的BackgroundColor

[英]Bind BackgroundColor of a view inside DataTemplate Xamarin.Forms

我有一個帶有DataTemplate的列表視圖,並且在其中有一個StackLayout,我需要設置它的背景,但是它的顏色在一個變量內...當我使用此變量的顏色以編程方式設置對象的背景時,它可以工作,但是當我嘗試在xaml中使用“綁定”時,它不起作用,並且無法以編程方式獲取此堆棧,因為它位於數據模板中……我真的需要您的答案..有幫助嗎?

<ListView.ItemTemplate>
                <DataTemplate>
                    <!--<DataTemplate.Triggers>
                            <DataTrigger Binding="{Binding fundoColor}" Value="4">
                                <Setter TargetName="produtos_stack_color" Property="Background" Value="LawnGreen" />
                            </DataTrigger>
                        </DataTemplate.Triggers>-->

                    <ViewCell>

                        <StackLayout x:Name="produtos_stack_color" BackgroundColor="{Binding fundoColor}" VerticalOptions="FillAndExpand" Margin="10,10,10,10">
                            <StackLayout Orientation="Horizontal" Padding="10,10,10,10" BackgroundColor="Black" HorizontalOptions="FillAndExpand">
                                <Image Source="{Binding imagem}" HeightRequest="80" HorizontalOptions="CenterAndExpand" WidthRequest="160" Aspect="Fill"/>

                                <StackLayout Orientation="Horizontal" BackgroundColor="Green" VerticalOptions="Center" HorizontalOptions="CenterAndExpand">
                                    <Label Style="{StaticResource labelsfont}" Text="R$" FontSize="Medium" TextColor="White"/>
                                    <Label Style="{StaticResource labelsfont}" Text="{Binding valor}" FontAttributes="Bold" FontSize="Large" TextColor="White"/>
                                </StackLayout>

                            </StackLayout>

                            <StackLayout Margin="0,0,0,10">
                                <Label Text="{Binding nome}" Style="{StaticResource labelsfont}" FontAttributes="Bold" FontSize="Medium" TextColor="White" VerticalOptions="StartAndExpand" HorizontalOptions="Center"/>
                                <ContentView BackgroundColor="Chartreuse" HorizontalOptions="FillAndExpand">
                                    <Label Style="{StaticResource labelsfont}" Text="{Binding observacao}" TextColor="White" Margin="10,10,10,10" HorizontalOptions="Center" />
                                </ContentView>
                            </StackLayout>

                        </StackLayout>
                    </ViewCell>
                </DataTemplate>

我聽說過觸發器...但是我真的不知道它是如何工作的...我需要produtos_stack_color接收顏色

我的全局變量在代碼后面...僅在我的類的構造中設置

InitializeComponent();
fundoColor =  Color.FromHex(this.categEscolhida.corFundo);

您只能綁定屬性-因此您將必須在代碼隱藏類中創建一個屬性。

public Color FundoColor { get { return fundoColor; } }

其次,為了在XAML中引用此屬性-您可以使用Reference擴展,並將parent指定為Source 例如:

<StackLayout x:Name="produtos_stack_color" 
             BackgroundColor="{Binding FundoColor, Source={x:Reference ParentHost}}" ..>

並且,請確保在XAML的根節點中設置x:Name屬性。 例如:

<ContentPage x:Name="ParentHost" .. />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM