Xaml 绑定DataTemplate Xamarin.Forms内视图的背景色

Xaml 绑定DataTemplate Xamarin.Forms内视图的背景色,xaml,xamarin,layout,xamarin.forms,Xaml,Xamarin,Layout,Xamarin.forms,我有一个带有DataTemplate的列表视图,其中有一个StackLayout,我需要设置它的背景,但它的颜色在一个变量中…当我使用这个变量颜色以编程方式设置对象的背景时,它可以工作,但当我尝试使用“绑定”时在xaml中,它不起作用,我无法以编程方式获取此堆栈,因为它位于数据模板中。。。 我真的需要你的回答。。 要帮忙吗 <ListView.ItemTemplate> <DataTemplate>

我有一个带有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>

您只能绑定属性-因此您必须在代码隐藏类中创建属性

public Color FundoColor { get { return fundoColor; } }
其次,为了在XAML中引用这个属性,您可以使用extension,并将parent指定为
Source
。例如:

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

当你提到fundoColor是一个变量时,它是在代码隐藏还是视图模型中定义的?代码隐藏我在这里发布它现在在那里它是一种混乱…但我现在正在尝试它哦,天哪!!!这真是一个胡克小子!!!非常感谢大家!!!你不知道你是怎么救了我的命(或者我的工作,用一个更好的方式说)谢谢你,没什么问题。很高兴知道答案。:)
<StackLayout x:Name="produtos_stack_color" 
             BackgroundColor="{Binding FundoColor, Source={x:Reference ParentHost}}" ..>
<ContentPage x:Name="ParentHost" .. />