How to declare a ternary condition in XAML as a parameter for a Custommarkupextension

Asked

Viewed 120 times

1

Hello I’m starting to work with WPF and created a Custom Markup Extension leaving of this tutorial. The intention of this Markup Extension is to simulate the declarative conditions that are found in Webframeworks such as Angular2 or the Vuejs for the web, for example:

<!-- se não atende a condição, não insere o elemento -->
<div *ngIf="condition">...</div> <!-- angular -->
<div v-if="condition">...</div>  <!-- vue -->

<!-- retorna para a variável -->
<input type="text" [disabled]="condition" /> <!-- angular -->
<input type="text" :disabled="condition" /> <!-- vue -->

<!-- atribui uma classe ao elemento -->
<div [ngClass]="{ 'foo': condition }"></div> <!-- angular -->
<div :class="{ 'foo': condition }"></div> <!-- vue -->

Like XAML is much like HTML and I already have a habit of working with these frameworks, what I try to do is simplify to the maximum the form of comparison by adding the variation of a variable from this condition. So I could for example change the text of a button, the visibility, the color, etc...

Below is my Markup:

namespace WPF
{
    public class If : MarkupExtension
    {
        public bool Condition { get; set; }
        public object True { get; set; }
        public object False { get; set; }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (Condition & (True != null && False != null))
                return Condition ? True : False;
            else
                throw new InvalidOperationException("Inputs cannot be blank");
        }
    }
}

The intention of this Markup Extension is to literally imitate the If operator for tenary operations, having its use like this:

<Window x:Class="WPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WPF">
    <Grid>

        <!-- ocultar ou mostrar -->
        <TextBlock Visibility="{fn:If Condition=/* ?? */, True='Visible', False='Colapsed'}"/>

        <!-- mudança de variável -->
        <TextBlock Text="{fn:If Condition=/* ?? */, True='Is true!', False='Is not true!'}"/>

        <!-- Mudança de estilo (como as classes html) -->
        <TextBlock Text="{fn:If Condition=/* ?? */, True={DynamicResource Foo}, False={DynamicResource Bar}"/>
    </Grid>
</Window>

My problem is that I don’t know how I can declare a condition directly in XAML using the Binding of a variable. Let’s imagine that in my class MainWindow.cs I have a variable like this:

namespace WPF
{
    public partial class MainWindow
    {
        private int randomNumber { get; set; }

        public MainWindow()
        {
            Random rnd = new Random();
            randomNumber = rnd.Next(0, 10);
        }
    }
}

How do I make a ternary comparison directly in XAML? For example, how do I check if the number is greater than 6, ie (randomNumber > 6 ? 'Is true!' : 'Is not true!')

Edit

I even found this solution but I do not agree that I am obliged to create all this to suit a simple ternary operation, after all if I have to create a class and a convert for each possibility/attribute, I would understand as a flaw in the language...

  • 1

    I believe that in that solution the proposal to use a Converter fit better to the problem. In the case of a ternary operator like Markup, the key point is to interpret the expression (in the case of randomNumber > 6).

  • yes, of course, the problem is that I need to interpret it in the value passage. ex: function foo(2 + 2).

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.