Forum Xamarin.Forms


The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

How to set a datatrigger inside a contentview to change its content?

CaioshinCaioshin ITMember ✭✭

I've a simple question: I want to change the control of a stacklayout, loading dynamically one of my controls declared in resources based on a value binded to a property.

Here is how I'm trying to achieve this result:

In my page I'm declaring my controltemplate as resource, with a datatrigger that should change the value of the ContentPresenter (but I'm not sure if this is the correct property that I have to change):

<ViewCell.Resources> <ControlTemplate x:Key="ucNumeric"> <local:PromptNumericDataTemplate></local:PromptNumericDataTemplate> <ControlTemplate.Triggers> <DataTrigger Binding="{Binding MyProp}" Value="X"> <Setter TargetName="ContentView" Property="ContentPresenter"> <Setter.Value> <local:PromptStringDataTemplate></local:PromptStringDataTemplate> </Setter.Value> </Setter> </DataTrigger> </ControlTemplate.Triggers> </ControlTemplate > </ViewCell.Resources>

then, I have a simple StackLayout where I want to load the content of my ControlTemplate, that should be different based to the value assigned to MyProp:

<StackLayout VerticalOptions="Fill" HorizontalOptions="Fill" Padding="12,0,12,0"> <ContentView x:Name="ContentView" ControlTemplate="{StaticResource ucNumeric}"></ContentView> </StackLayout>

but it doesn't work.
Can somebody tell me what's the right way to achieve my goal?

Thank you


Sign In or Register to comment.