UWP Show a check symbol when boolean is true

时间:2016-08-31 12:17:26

标签: c# windows uwp uwp-xaml

I have a ListView with all kind of tasks that have to be completed. When a task is completed a user presses on the item in the listview. This changes a boolean in my web api. Now I want to display a check symbol on the items that are already completed when I get all the items from my API.

<SymbolIcon Grid.Column="0" x:Name="CompleteTask" Symbol="Accept"/>
<TextBlock Name="TaakNaam" 
    Grid.Column="1"
    Text="{x:Bind TaskName}" 
    FontSize="16"/>

What it should be

2 个答案:

答案 0 :(得分:0)

you can write property

public Visibility TickVisibility
{
    get {return YourBoolean == true ? Visibility.Visible : Visibility.Collapsed;}
}

and Bind it:

<SymbolIcon Grid.Column="0" x:Name="CompleteTask" Symbol="Accept" Visibility={Binding TickVisibility}/>

or use NotifyProperty every time YourBoolean has changed or write Converter that will implement IValueConverter and will convert bool<=>Visibility

答案 1 :(得分:0)

我创建了这个转换器:

public class BooleanToVisibilityConverter : IValueConverter
{
    public BooleanToVisibilityConverter()
    {
    }

    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (value is bool && (bool) value)
        {
            return Visibility.Visible;
        }
        return Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        return (value is Visibility && (Visibility)value == Visibility.Visible);
    }
}

并在我看来导入了这个:

<Page.Resources>
    <vm:BooleanToVisibilityConverter x:Key="BoolToVis" />
</Page.Resources>

我添加了一个绑定到我的SymbolIcon:

<SymbolIcon Grid.Column="0" x:Name="CompleteTask" Symbol="Accept" Foreground="Green" Visibility="{Binding Path=Check, Converter=StaticResource BoolToVis}}"/>

来源:https://pmdevweb.wordpress.com/2016/05/24/uwp-binding-boolean-to-visibility/