Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adaptative Triggers with custom attached properties in Windows 10 UWP

I am trying to set in the visual states a custom attached property I have tried several ways with the complete namespace, without, with the alias, etc. without success any idea?

xmlns:p="using:Controls.Views.Properties"
<RelativePanel x:Name="RootPanel" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="VisualStateGroup">
            <VisualState x:Name="NarrowView">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="0" />
                </VisualState.StateTriggers>
                <VisualState.Setters>
     //This is where the value is not achieved
     <Setter Target="Text.(p:RelativeSize.Width)" Value="0.5"/>

It does not just fire any error, I can replace p: with j: and it doesn't crash, I do not know how to solve that.

This question is to finish the solution in here: Relative width for UI Elements with RelativePanel in XAML with UWP Apps

like image 407
Juan Pablo Garcia Coello Avatar asked Nov 22 '22 09:11

Juan Pablo Garcia Coello


1 Answers

There is a way using the event and reflection:

XAML:

<VisualStateGroup x:Name="VisualStateGroup" CurrentStateChanged="VisualStateGroup_CurrentStateChanged">

Code:

private void VisualStateGroup_CurrentStateChanged(object sender, VisualStateChangedEventArgs e)
    {
        foreach (var sbase in e.NewState.Setters)
        {
            var setter = sbase as Setter;
            var spath = setter.Target.Path.Path;
            var element = setter.Target.Target as FrameworkElement;

            if (spath.Contains(nameof(RelativeSize)))
            {
                string property = spath.Split('.').Last().TrimEnd(')');

                var prop = typeof(RelativeSize).GetMethod($"Set{property}");

                prop.Invoke(null, new object[] { element, setter.Value });
            }
        }
    }

It solves this case and you can adapt to multiple cases

like image 76
Juan Pablo Garcia Coello Avatar answered Feb 15 '23 20:02

Juan Pablo Garcia Coello