空时可见转换器

时间:2019-02-24 05:24:46

标签: c# xaml uwp template10

如何使转换器成为VisibleWhenEmptyString。我做了如下,但它不起作用。问候

    <converters:ValueWhenConverter x:Key="VisibleWhenEmptyString">
    <converters:ValueWhenConverter.When>
        <x:String ></x:String>
    </converters:ValueWhenConverter.When>
    <converters:ValueWhenConverter.Value>
        <Visibility>Visible</Visibility>
    </converters:ValueWhenConverter.Value>
    <converters:ValueWhenConverter.Otherwise>
        <Visibility>Collapsed</Visibility>
    </converters:ValueWhenConverter.Otherwise>
</converters:ValueWhenConverter>

1 个答案:

答案 0 :(得分:0)

如果您直接使用Template10源代码调试ValueWhenConverter,则在设置<x:String ></x:String>时会看到它实际上是一个空值。

enter image description here

因此,当您的绑定路径值为string.Empty时,您可以将其直接更改为null。这是解决此问题的最简单方法。

<Page.Resources>
    <converters:ValueWhenConverter x:Key="VisibleWhenTrueConverter">
        <converters:ValueWhenConverter.When>
            <x:String></x:String>
        </converters:ValueWhenConverter.When>
        <converters:ValueWhenConverter.Value>
            <Visibility>Visible</Visibility>
        </converters:ValueWhenConverter.Value>
        <converters:ValueWhenConverter.Otherwise>
            <Visibility>Collapsed</Visibility>
        </converters:ValueWhenConverter.Otherwise>
    </converters:ValueWhenConverter>
</Page.Resources>
<Grid>
    <TextBlock Text="Hello Admin" Visibility="{x:Bind IsAdmin, Converter={StaticResource VisibleWhenTrueConverter}}" />
</Grid>
public sealed partial class MainPage : Page
{
    private string _IsAdmin;
    public string IsAdmin
    {
        get { return _IsAdmin; }
        set
        {
            if (value == string.Empty)
            {
                _IsAdmin = null;
            }
            else
            {
                _IsAdmin = value;
            }
        }
    }
    public MainPage()
    {
        this.InitializeComponent();
        IsAdmin = string.Empty; ;
    }
}