-1

I want to display the style of a control in a template depending on an enum in the used class. I tried to use this to use the enum in XAML and this to create a trigger. The problem is that I cannot use x:Static in UWP and the trigger is never fired. My workaround does not work either.

My class:


//Namespace Enums
public enum ConnectionState
{
    Open,
    Closed,
    Connecting,
    Broken
}

//Namespace Models
public class DatabaseConnection : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    private ConnectionState _connectionState = ConnectionState.Broken;
    public ConnectionState ConnState
    {
        get => _connectionState;
        set
        {
            if (value != _connectionState)
            {
                _connectionState = value;
                OnPropertyChanged();
                OnPropertyChanged(nameof(ConnStateInt));
                OnPropertyChanged(nameof(InfoBadgeStyle));
            }
        }
    }

    public int ConnStateInt => (int)ConnState;

    public Style InfoBadgeStyle
    {
        get
        {
            return ConnState switch
            {
                ConnectionState.Open => (Style)Application.Current.Resources["SuccessIconInfoBadgeStyle"],
                ConnectionState.Connecting => (Style)Application.Current.Resources["AttentionIconInfoBadgeStyle"],
                ConnectionState.Broken => (Style)Application.Current.Resources["CriticalIconInfoBadgeStyle"],
                _ => (Style)Application.Current.Resources["InformationalIconInfoBadgeStyle"],
            };
        }
    }
}

My template:

<Page.Resources>
    <DataTemplate x:Key="ConnectionTemplate" x:DataType="models:DatabaseConnection">
        <muxc:InfoBadge Style="{x:Bind InfoBadgeStyle}"/>
    </DataTemplate>
</Page.Resources>

How can I update the style with a trigger in UWP?

tomo_2403
  • 15
  • 5

1 Answers1

1

It's unclear why you have both an InfoBadgeStyle property and triggers in XAML but if you want your ChangePropertyActions to be able to set the Style property, you should not set the local Style property like this as it will take precedence:

<muxc:InfoBadge ... Style="{x:Bind InfoBadgeStyle}">

So either remove the InfoBadgeStyle property or remove the triggers.

Also note that x:Bind binds OneTime by default so if you want to react to change notifications you should set the Mode to OneWay: Style="{x:Bind InfoBadgeStyle, Mode=OneWay}"

mm8
  • 163,881
  • 10
  • 57
  • 88