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
2021-01-26T21:29:49.247+00:00

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

60693-image.png

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.

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

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 78,836 Reputation points Microsoft Vendor
    2021-01-27T06:19:32.257+00:00

    Hello,​

    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.

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

    Then I create a contentView called View1.Xaml.

       <ContentView xmlns="http://xamarin.com/schemas/2014/forms"   
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"   
                    xmlns:local="clr-namespace:App27"  
                    x:Class="App27.View1">  
         <ContentView.Content>  
         
               <StackLayout x:DataType="local:MyViewModelColor">  
                   <StackLayout.BindingContext>  
                       <local:MyViewModelColor />  
                   </StackLayout.BindingContext>  
                     
                     
                   <Frame Padding="24" CornerRadius="0" Style="{StaticResource ProductFrame}">  
                       <Label Text="Welcome to Xamarin.Forms!" HorizontalTextAlignment="Center" TextColor="White" FontSize="36"/>  
                   </Frame>  
               </StackLayout>  
         </ContentView.Content>  
       </ContentView>  
    

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

    60817-image.png

    It worked, here is my running screenshot.

    60835-image.png

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.