.NET MAUI Triggers

Triggers in .NET MAUI are not that different from triggers in Xamarin.Forms, but since this is not a frequently used feature, I thought I’d provide a quick deep-dive into their usage.

Triggers allow you to declare, in your XAML, how a control should appear based on data changes. You can combine state triggers with Visual State, a topic I’ll cover in a subsequent blog post.

This is the first of a series of short pieces based on material in my forthcoming book .NET MAUI For C# Developers (Packt* Publishing) which we are targeting for publication in early April.

For example, suppose we have a create account page, and we want the create button to be disabled if the user has not filled in a password. You can certainly do this in code (and .NET MAUI Community Toolkit will make this very easy with behaviors), but you can also do it declaratively in XAML using a DataTrigger.

<Button
    Command="{Binding DoCreateAccountCommand}"
    Grid.Column="1"
    Grid.Row="2"
    Style="{StaticResource LoginButton}"
    Text="Create Account">
    <Button.Triggers>  
        <DataTrigger
            Binding="{Binding Source={x:Reference passwordEntry}, Path=Text.Length}"
            TargetType="Button"
            Value="0"> 
            <Setter Property="IsEnabled" Value="False" /> 
        </DataTrigger>
    </Button.Triggers>
</Button>

Notice that the Button has a collection of Triggers. The type we want is a DataTrigger (as opposed, for example, to an event trigger). The trigger binds to the passwordEntry field’s Text.Length and looks for the value 0. If that is true, then the property IsEnabled is set to false. Once the Text.Length is not zero, the property is set to true.

That’s all it takes. You can see that triggers are often quite simple, but for some reason considered “advanced” and thus not used often; which is a shame because they can greatly simplify your code and make the logic easier to understand.

Note that the field you are checking (in this case password) must have its Text initialized to “” (in the ViewModel or code-behind, but of course the ViewModel is the right place). Otherwise it will be null, and the trigger may not act as expected.

In my next tutorial, I’ll tackle the more interesting and complex subject of Visual States.

———–

*This being my second book from Packt, I decided to ask how it is pronounced. Turns out it is one syllable, sounding more like packed than like packet. If you want to get it really right, say the word pack and sneak in a t sound at the end. (Just thought you’d want to know.)

Unknown's avatar

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen online courses. Liberty is a Senior AI Engineer at the University of Pittsburgh Medical Center, and was a Team Lead and Senior Software Engineer for various corporations, a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a 21 year Microsoft MVP.
This entry was posted in Essentials. Bookmark the permalink.

4,198 Responses to .NET MAUI Triggers

  1. 8kbet's avatar 8kbet says:

    8KBET hoạt động theo mô hình nhằm đảm bảo sự ổn định.

  2. 8kbet's avatar 8kbet says:

    Tham gia 8KBET để khám phá nền tảng giải trí trực tuyến chuyên nghiệp.

  3. 8kbet's avatar 8kbet says:

    8KBET được phát triển với mục tiêu bền vững.

  4. 8kbet's avatar 8kbet says:

    8KBET mang lại độ tin cậy trong quá trình tham gia.

  5. 8kbet's avatar 8kbet says:

    8KBET không ngừng hoàn thiện nền tảng dịch vụ.

  6. This post was incredibly well structured.

  7. Your ability to explain things simply is such a gift.

  8. Franksnomo's avatar Franksnomo says:

    Срочный выкуп авто становится всё очень популярной услугой.
    Данный сервис дает шанс продавцу быстро обрести наличные за автомобиль.
    Многих подкупает отсутствие времени на изнурительные переговоры с покупателями.
    Фирмы оценивают авто на выезде и предлагают адекватную цену.
    Подобный подход избавляет от сложностей с бумажной волокитой и гарантирует безопасность сделки.
    https://www.shippingexplorer.net/ru/user/kenigauto39/255397

  9. Richardquatt's avatar Richardquatt says:

    Dressing stylish creates a strong first impression.
    A outfit communicates volumes before a person actually speak a single word.
    This enhances your own confidence and mood significantly.
    Your put-together appearance conveys professionalism in the workplace.
    https://list.chenews.ru/hGEnbD2i/
    Fashionable clothing allow you to express your unique personality.
    People often judge well-dressed people as more capable and reliable.
    Therefore, putting effort in your wardrobe is an investment in yourself.

  10. Richardquatt's avatar Richardquatt says:

    Dressing stylish creates a strong first impression.
    A attire speaks loudly before a person even say a single word.
    It enhances your personal self-esteem and mood significantly.
    A put-together appearance conveys competence in the office.
    https://a3.megakazan.ru/gkwCIBv/
    Fashionable clothing allow you to express your individual personality.
    People often judge stylish people as more successful and reliable.
    Therefore, investing in your style is an valuable step in your personal brand.

  11. Richardquatt's avatar Richardquatt says:

    Dressing well creates a positive first impression.
    A outfit communicates volumes before a person even speak a single word.
    It boosts your personal self-esteem and mindset significantly.
    Your put-together look signals professionalism in the workplace.
    https://blog.chenews.ru/sumka-kak-personazh-pochemu-furla-ne-vykhodit-iz-spektaklya/
    Fashionable clothing let you to express your individual identity.
    People often perceive stylish people as more capable and trustworthy.
    Ultimately, putting effort in your wardrobe is an investment in yourself.

  12. Richardquatt's avatar Richardquatt says:

    Looking well projects a positive first impression.
    Your attire communicates loudly before you actually speak a word.
    This enhances your personal self-esteem and mindset noticeably.
    Your put-together appearance signals competence in the workplace.
    https://b.balenciager.ru/V6lgz5gdrb/
    Fashionable choices allow you to express your individual identity.
    Others often perceive stylish people as more capable and reliable.
    Ultimately, putting effort in your style is an investment in your personal brand.

  13. WilliamCig's avatar WilliamCig says:

    High-end timepieces continue to remain popular despite the rise of digital devices.
    They are often seen as a mark of success and refined taste.
    Craftsmanship plays a major role in their lasting value.
    Many luxury watches are produced using premium components.
    https://miafashion.review-blogger.com/61245387/acne-studios-dresses-the-quiet-intelligence-of-clothing
    They also represent a long tradition passed down through generations.
    For collectors, these watches can serve as both wearable tools and valuable assets.
    Timeless design allows them to stay relevant across changing fashion trends.
    Ultimately, luxury watches continue to interest buyers around the world.

  14. The energy here is unmatched — you crushed it!

  15. Richardquatt's avatar Richardquatt says:

    Dressing well creates a strong first impression.
    Your outfit speaks volumes before you actually say a single word.
    This boosts your own self-esteem and mood noticeably.
    A put-together appearance conveys competence in the office.
    https://www.tumblr.com/sneakerizer/779513865541140480/%D1%81%D0%B5%D0%BC%D1%8C-%D0%BB%D1%83%D1%87%D1%88%D0%B8%D1%85-%D0%BC%D0%BE%D0%B4%D0%B5%D0%BB%D0%B5%D0%B9-%D1%81%D1%83%D0%BC%D0%BE%D0%BA-furla
    Stylish clothing allow you to express your unique identity.
    People often perceive well-dressed people as more successful and trustworthy.
    Therefore, putting effort in your style is an valuable step in your personal brand.

  16. Richardquatt's avatar Richardquatt says:

    Dressing stylish projects a positive first impression.
    Your attire communicates loudly before a person actually speak a single word.
    It enhances your own confidence and mindset significantly.
    A put-together look signals competence in the workplace.
    https://bookmarks.balmain1.ru/1CWKXUq3DbG/
    Fashionable choices allow you to showcase your individual personality.
    Others often perceive stylish individuals as more successful and reliable.
    Therefore, investing in your wardrobe is an investment in yourself.

  17. Richardquatt's avatar Richardquatt says:

    Dressing stylish creates a strong first impression.
    Your outfit communicates loudly before a person even speak a single word.
    It enhances your personal confidence and mindset significantly.
    A polished look signals competence in the office.
    https://n2.sneakero.ru/dSOy7zDs1nZN/
    Fashionable clothing let you to showcase your individual identity.
    Others often perceive well-dressed individuals as more successful and trustworthy.
    Therefore, putting effort in your wardrobe is an investment in your personal brand.

  18. AndreTag's avatar AndreTag says:

    Exchange for travel, best thc drinks has been carefree to gang and store. The hem in seals closely, and there’s no leakage. The minimalist design looks able and diplomatic, which makes it well off to broadcast around.

  19. Jeffreydag's avatar Jeffreydag says:

    I tried a not many products from Tillmans Tranquils – https://www.tillmanstranquils.com/products/cbd-sleep-gummies and really liked the complete experience. The gummies have a deplete b empty leaning, undisturbed surface, and in harmony quality. The flavors stroke reasonable, and the portioned servings up it comfortable to prefer what works in behalf of you. Their packaging looks premium and everything feels thoughtfully made. A solid brand with products that are enjoyable and reliable.

  20. SO79 mang đến trải nghiệm đơn giản cho người tham gia.

  21. SO79 là một trong những nền tảng được quan tâm hiện nay.

  22. Với giao diện thân thiện, SO79 giúp người chơi trải nghiệm thuận tiện hơn.

  23. so79's avatar so79 says:

    SO79 mang đến trải nghiệm đầy đủ cho người chơi.

  24. so79's avatar so79 says:

    SO79 cam kết tính minh bạch trong quy trình vận hành.

  25. SO79 mang đến trải nghiệm toàn diện cho thành viên.

  26. so79's avatar so79 says:

    SO79 tạo điều kiện để thành viên trải nghiệm giải trí online dễ dàng.

  27. so79's avatar so79 says:

    SO79 là nền tảng cá cược online uy tín, cung cấp trải nghiệm an toàn.

  28. so79's avatar so79 says:

    SO79 giúp người dùng tham gia mọi lúc.

  29. so79's avatar so79 says:

    SO79 phù hợp với xu hướng online hiện nay.

  30. SO79 được xây dựng dựa trên hệ thống phù hợp xu hướng online.

  31. link so79's avatar link so79 says:

    SO79 phát triển nhằm đáp ứng nhu cầu cá cược phổ biến của thành viên.

  32. Morrisville Auto Glass's avatar Morrisville Auto Glass says:
  33. so79's avatar so79 says:

    SO79 hoạt động theo mô hình nhằm đảm bảo sự ổn định.

  34. so79's avatar so79 says:

    SO79 mang đến không gian cá cược công bằng.

  35. Morrisville Auto Glass's avatar Morrisville Auto Glass says:

    This is content worth celebrating — phenomenal job!

  36. This post was absolutely fantastic from start to finish.

  37. SO79 luôn đặt trải nghiệm người dùng lên mức cao nhất.

  38. SO79 được xây dựng nhằm đáp ứng nhu cầu cá cược đa dạng của người chơi.

  39. link so79's avatar link so79 says:

    SO79 là địa chỉ giải trí online được nhiều người lựa chọn.

  40. so79's avatar so79 says:

    SO79 phát triển nhằm phục vụ nhu cầu giải trí đa dạng của người chơi.

  41. so79's avatar so79 says:

    SO79 mang đến giá trị thực tế cho người tham gia.

  42. SO79 cam kết duy trì chất lượng hệ thống ổn định.

  43. SO79 là hệ thống cá cược online ổn định, cung cấp trải nghiệm minh bạch.

  44. SO79 mang lại quá trình sử dụng liền mạch trên nhiều nền tảng.

  45. Tham gia SO79, thành viên có thể nhận ưu đãi hấp dẫn.

  46. so79's avatar so79 says:

    SO79 phù hợp với thị trường online hiện nay.

  47. so79's avatar so79 says:

    SO79 tạo điều kiện để thành viên tiếp cận giải trí online dễ dàng.

  48. SO79 tập trung xây dựng hệ thống có độ ổn định cao.

  49. link so79's avatar link so79 says:

    SO79 là nền tảng hướng tới chất lượng dịch vụ.

  50. SO79 được xây dựng nhằm phục vụ nhu cầu trải nghiệm online ngày càng cao.

Comments are closed.