如何修复错误 “XLS0432:在WPF中找不到类型 ‘String’ 中的属性 ‘IsEmpty'”。

huangapple go评论71阅读模式
英文:

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:

&lt;TextBox x:Name=&quot;txtSearch&quot; /&gt;

&lt;TextBlock Text=&quot;{Binding Text.Length, ElementName=txtSearch}&quot;&gt;
    &lt;TextBlock.Style&gt;
        &lt;Style TargetType=&quot;TextBlock&quot;&gt;
            &lt;Setter Property=&quot;Visibility&quot;
                    Value=&quot;Visible&quot; /&gt;
            &lt;Style.Triggers&gt;
                &lt;DataTrigger Binding=&quot;{Binding Text.Length, ElementName=txtSearch}&quot; Value=&quot;0&quot;&gt;
                    &lt;Setter Property=&quot;Visibility&quot;
                            Value=&quot;Hidden&quot; /&gt;
                &lt;/DataTrigger&gt;
            &lt;/Style.Triggers&gt;
        &lt;/Style&gt;
    &lt;/TextBlock.Style&gt;
&lt;/TextBlock&gt;

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 &amp;&amp;
            !string.IsNullOrWhiteSpace(strValue))
        {
            return Visibility.Visible;
        }

        return Visibility.Hidden;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

huangapple
  • 本文由 发表于 2023年7月11日 00:42:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655750.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定