Assign enum property in xaml using silverlight

I have a datatype enumeration property: for example

public BreakLevel Level
{
    get { return level; }
    set { level = value; }
}

      

And the enum is defined:

  public enum BreakLevel
    {
        Warning, Fatal
    }

      

I want to bind the neum property to the visibility of my border, something like this:

Visibility = "{Binding BreakLevel.Fatal}" is this possible?

<Border CornerRadius="4" BorderThickness="1"  BorderBrush="#DAE0E5"  
Visibility="{Binding DataContext.IsError, Converter={StaticResource BoolToVisibilityConverter}, RelativeSource={RelativeSource TemplatedParent}}" >

      

+2


a source to share


4 answers


I think you can just create BreakLevelToVisibilityConverter and link exactly like in the example you provided.

I am assuming that your boundary's DataContext is set to an instance of a class that has a property of type "BreakLevel" (we will call this property "BreakLvlProperty").

Then the below code will show the border if the BreakLvlProperty value is BreakLevel.Fatal

Converter



public class BreakLevelToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if ((BreakLevel)value == BreakLevel.Fatal)
            return Visibility.Visible;
        else
            return Visibility.Hidden;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
}

      

XAML:

<TopLevelWindowOrControl.Resources>
    <local:BreakLevelToVisibilityConverter x:Key="BreakLevelToVisibilityConverter" />
</TopLevelWindowOrControl.Resources>

<Border Visibility="{Binding Path=BreakLvlProperty, Converter={StaticResource BreakLevelToVisibilityConverter}" />

      

+3


a source


Scott has a good answer to a real question, but it's always a good idea to ask yourself, "How might I need the code , for example in the future? How can I avoid creating another class and reuse what I already have instead?"

Here is a more general variation on Scott's solution: -

public class EnumToVisibilityConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
        if (Enum.GetName(value.GetType(), value).Equals(parameter)) 
            return Visibility.Visible; 
        else 
            return Visibility.Hidden; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
        return null; 
    } 
} 

      

Xaml: -



<TopLevelWindowOrControl.Resources>   
    <local:EnumToVisibilityConverter x:Key="EnumToVisibilityConverter" />   
</TopLevelWindowOrControl.Resources>   

<Border Visibility="{Binding Path=BreakLvlProperty, Converter={StaticResource EnumToVisibilityConverter}, ConverterParameter=Fatal" />

      

In this approach, we can convert any enumeration to a Visibility value by using ConverterParameter

to specify the value of the enumerations (as a string) that make up the Visible state.

It makes him wonder to allow more than one enumeration value to be equated to "Visible". However, the code is currently not much more complex than Scott's, more specific implementation. Therefore, this improvement should be kept until needed.

+4


a source


public class EnumToVisibilityConvertor : IValueConverter
{
    private bool chk;
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if ((value != null) && (value is BreakLevel) && (targetType == typeof(Visibility)))
        {
          chk =   ((((BreakLevel) value) == (BreakLevel) Enum.Parse(typeof (BreakLevel), parameter.ToString(), true)));
          return (chk==true) ? Visibility.Visible : Visibility.Collapsed;
        }

        throw new InvalidOperationException("Invalid converter usage.");
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;

    }
}





      <Border CornerRadius="4" BorderThickness="1"  BorderBrush="#DAE0E5"  
Visibility="{Binding Path=Level, Converter={StaticResource enumToVisibilityConvertor},ConverterParameter=Fatal}" >

      

0


a source


I know this is a little old question, but you are all focused on converters and there is a much better (in my opinion) way to do it without involving code:

    <ContentControl>
        <ContentControl.Template>
            <ControlTemplate>
                <Grid>
                    <!-- Here is the border which will be shown if Object.Level has value Fatal -->
                    <Border x:Name="PART_Border"
                        Visibility="Collapsed"
                        BorderThickness="3" BorderBrush="Red"
                        CornerRadius="4">
                    </Border>
                    <TextBlock>Interiors of the border</TextBlock>
                </Grid>
                <ControlTemplate.Triggers>
                    <!-- This is the code which turns on border visibility -->
                    <DataTrigger Binding="{Binding Level}" Value="{x:Static local:BreakLevel.Fatal}">
                        <Setter TargetName="PART_Border" Property="Visibility" Value="Visible" />
                    </DataTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </ContentControl.Template>
    </ContentControl>

      

I assumed that an object with a Level property arises in the DataContext, which is of your type BreakLevel.

0


a source







All Articles