繁体   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