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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With