简体   繁体   中英

WPF Binding using a default value

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

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.

XAML

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

MODEL

private string _myColor = "Red";

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM