How can I utilize compiled binding inside Application.Resources for say a StyleTrigger of type DataTrigger bound to a feild in the ViewModel?

Tyler Tucker 1 Reputation point

For example, If I have this Style block inside my Application.Resources.


and lets say inside the contentview where I have the ViewModel binded and DataType set (x:DataType"ViewModels:MyViewModel"), let's say I have a

<StackLayout Style="{StaticResource ProductFrame}">

With compiled binding, shouldn't that Product.Title property from the ViewModel be recognized? Is this a bug or a limitation because the DataType doesn't seem to apply to anything inside Style blocks and it doesn't seem to pass down to Static Style resources when applied to a parent view in the page xaml. However, the data does of course bind no problem.

A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,301 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 69,386 Reputation points Microsoft Vendor


    Welcome to our Microsoft Q&A platform!

    lets say inside the contentview where I have the ViewModel binded and DataType set (x:DataType"ViewModels:MyViewModel"),

    Firstly, I add following styles in Application.Resources, for testing, I set the Frame's background to red, BorderColor to Green.

               <Style x:Key="ProductFrame" TargetType="{x:Type Frame}">  
                       <DataTrigger TargetType="{x:Type Frame}" Binding="{Binding TestProperty}" Value="True">  
                           <Setter  Property="BackgroundColor" Value="Red"/>  
                           <Setter Property="BorderColor" Value="Green"></Setter>  

    Then I create a contentView called View1.Xaml.

       <ContentView xmlns=""   
               <StackLayout x:DataType="local:MyViewModelColor">  
                       <local:MyViewModelColor />  
                   <Frame Padding="24" CornerRadius="0" Style="{StaticResource ProductFrame}">  
                       <Label Text="Welcome to Xamarin.Forms!" HorizontalTextAlignment="Center" TextColor="White" FontSize="36"/>  

    MyViewModelColor.cs just have a property called TestProperty that used in ProductFrame's style.


    It worked, here is my running screenshot.


    Best Regards,

    Leon Lu

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.