How to convert style trigger WPF to UWP

Viewed 50

I want convert to WPF triigger to UWP. How to convert UWP, because the trigger is not available in the UWP

<TextBlock Text="Lorem ipsum dolor sit">
<TextBlock.Style>
    <Style TargetType="{x:Type TextBlock}">
        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Foreground"
                        Value="{StaticResource {x:Static SystemColors.GrayTextBrushKey}}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</TextBlock.Style>

</TextBlock

1 Answers

I want convert to WPF triigger to UWP. How to convert UWP, because the trigger is not available in the UWP

For UWP platform, you could use XamlBehaviors DataTriggerBehavior to implement Trigger functuion. But I have to say, UWP TextBlock has no IsEnabled property. So the following code using IsTextSelectionEnabled property to make a sample.

<TextBlock
    x:Name="Text"
    IsTextSelectionEnabled="False"
    Text="Hello World !">
    <Interactivity:Interaction.Behaviors>
        <Interactions:DataTriggerBehavior
            Binding="{Binding IsTextSelectionEnabled, ElementName=Text}"
            ComparisonCondition="Equal"
            Value="False">
            <Interactions:ChangePropertyAction
                PropertyName="Foreground"
                TargetObject="{Binding ElementName=Text}"
                Value="Red" />
        </Interactions:DataTriggerBehavior>

        <Interactions:DataTriggerBehavior
            Binding="{Binding IsTextSelectionEnabled, ElementName=Text}"
            ComparisonCondition="Equal"
            Value="True">
            <Interactions:ChangePropertyAction
                PropertyName="Foreground"
                TargetObject="{Binding ElementName=Text}"
                Value="Green" />
        </Interactions:DataTriggerBehavior>
    </Interactivity:Interaction.Behaviors>
</TextBlock>

UWP TextBox has IsEnabled property, and if you want to edit Foreground for disable state. you need to update the default style and change Disabled VisualState, And TextBox disable foreground has specific resource TextControlForegroundDisabled, the easy way is using the following to override the default one in app.xaml

<SolidColorBrush x:Key="TextControlForegroundDisabled" Color="SlateBlue" />
Related