0

I am using Binding to define three different colors to fill an ellipse

So to do that in my binding, I have used converter that contains an Enum

According to the enum returned, the fill color is changed

Some of my XAML :

<Ellipse Name="SignalStatus" Height="16" Width="16" Margin="29,35,14.2,68.2">
        <Ellipse.Style>
            <Style TargetType="Ellipse">
                <!--<Setter Property="Fill" Value="Red"/> -->
                <Style.Triggers>
                    <!--SignalStatus "Unknown" -->
                    <DataTrigger Binding="{Binding SignalStatus, Converter={StaticResource IntToSignalStatus} }" Value="Unknown">
                        <Setter Property="Fill" Value="Magenta"/>
                    </DataTrigger>
                    <!--SignalStatus "Permissive" -->
                    <DataTrigger Binding="{Binding SignalStatus, Converter={StaticResource IntToSignalStatus} }" Value="Permissive">
                        <Setter Property="Fill" Value="Green"/>
                    </DataTrigger>
                    <!--SignalStatus "Restrictive" -->
                    <DataTrigger Binding="{Binding SignalStatus, Converter={StaticResource IntToSignalStatus} }" Value="Restrictive">
                        <Setter Property="Fill" Value="Red"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Ellipse.Style>
    </Ellipse>

Converter:

public class IntToSignalStatus : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
            return SignalStatus.Unknown;

        switch (value.ToString())
        {
            case "0":
                return SignalStatus.Restrictive;

            case "1":

                return SignalStatus.Permissive;

            default:
                break;
        }

        return PlatformSkip.Unknown;

    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

In the specification docs they want the Red color to be set on the control by default.

How can I define a default fill color ( the Red one) for my ellipse ?

PS: I'm beginner en WPF & C# Programming

9
  • 4
    Uncomment the setter? But if the enum only contains the Unknown, Pemissive and Restrictive options, there default value will only be applied if the binding fails for some reason. Commented Jun 15, 2018 at 9:12
  • I commented the setter because i'm not sure if it's correct. I thought that is the way to set a default value. but when i Run the project color displayed by default is Green which corresponds to Permissive Status in the converter Commented Jun 15, 2018 at 9:22
  • 1
    This is the expected behaviour if the SignalStatus returns Restrictive. When do you expect the "default" colour to be displayed? The DataTriggers override the default setter. Commented Jun 15, 2018 at 9:27
  • i expect the control displays the Red color (default) when i lunch my application. Commented Jun 15, 2018 at 9:36
  • 1
    It should unless the SignalStatus property returns either Permissive or Unknown. Then it should be Green or Magenta respectively. That's the behaviour you have implemented in your XAML. In other words, the colour can never be Red if the SignalStatus property is set to Permissive or Unknown. Commented Jun 15, 2018 at 9:41

2 Answers 2

1

Your converter should return Restrictive for the Fill property of the Ellipse to be set to Red. This will trigger your last DataTrigger:

<DataTrigger Binding="{Binding SignalStatus, Converter={StaticResource IntToSignalStatus} }" Value="Restrictive">
    <Setter Property="Fill" Value="Red"/>
</DataTrigger>

Only if the converter returns neither Unknown, Permissive nor Restrictive, your default (uncommented) setter will apply.

Sign up to request clarification or add additional context in comments.

Comments

0

XAML

<Ellipse Fill="{Binding MyColor}"></Ellipse>

MODEL

private string _myColor = "Red";

public string MyColor
{
    get { return _myColor; }
    set
    {
        _myColor = value;
        OnPropertyChanged("");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.