英文:
How to fix Error "XLS0432 The property 'IsEmpty' was not found in type 'String' in WPF
问题
我正在创建一个搜索框,在这个过程中,我在以下代码行中遇到了一个错误:
Visibility="{Binding ElementName=txtSearch, Path=Text.IsEmpty, Converter={StaticResource BoolToVis}}"
具体来说,在 "Path=Text.IsEmpty" 下面。我认为这个错误发生是因为搜索框的值有时会为 null,但我不知道如何解决它。
只要错误出现,我仍然可以编译和运行我创建搜索框的应用程序,但搜索框的设计不符合我的要求。它显示了我创建的搜索框的骨架,而不是我为其命名为 'txtSearch' 的样式。
英文:
I am making a search box and in the process I get an error in the line
Visibility="{Binding ElementName=txtSearch, Path=Text.IsEmpty, Converter={StaticResource BoolToVis}}"
Specifically under Path=Text.IsEmpty. I am assuming this error happens because the value of the searchBox will be null at times but I have no idea how to get rid of it.
As long as the error appears I can still compile and run the applicaiton for which I am making the search box but the design of the search box is not what I want. It shows the skeleton for the search box I made instead of the style I made for it called 'txtSearch'.
答案1
得分: 1
你可以使用 Text.Length 在 DataTrigger 中根据 TextBox 是否为空来控制控件的可见性:
<TextBox x:Name="txtSearch" />
<TextBlock Text="{Binding Text.Length, ElementName=txtSearch}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Visibility" Value="Visible" />
<Style.Triggers>
<DataTrigger Binding="{Binding Text.Length, ElementName=txtSearch}" Value="0">
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
你也可以为此创建一个自定义的 EmptyToHidden
转换器,这不需要使用样式:
public class EmptyToHidden : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string strValue &&
!string.IsNullOrWhiteSpace(strValue))
{
return Visibility.Visible;
}
return Visibility.Hidden;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
英文:
You can control the Visibility of a control based on a TextBox being empty by using Text.Length in a DataTrigger:
<TextBox x:Name="txtSearch" />
<TextBlock Text="{Binding Text.Length, ElementName=txtSearch}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Visibility"
Value="Visible" />
<Style.Triggers>
<DataTrigger Binding="{Binding Text.Length, ElementName=txtSearch}" Value="0">
<Setter Property="Visibility"
Value="Hidden" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
You could also create a custom EmptyToHidden
Converter for this which would not require a Style:
public class EmptyToHidden : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string strValue &&
!string.IsNullOrWhiteSpace(strValue))
{
return Visibility.Visible;
}
return Visibility.Hidden;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论