如何使转换器成为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>
答案 0 :(得分:0)
如果您直接使用Template10源代码调试ValueWhenConverter,则在设置<x:String ></x:String>
时会看到它实际上是一个空值。
因此,当您的绑定路径值为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; ;
}
}