wpf 空字符串上的数据触发器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19236137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:48:11  来源:igfitidea点击:

Datatrigger on empty string

c#wpfdata-binding

提问by Giffesnaffen

How can a DataTrigger change the visibility of stackpanel, based on a binded string? I have the following Xaml

DataTrigger 如何根据绑定字符串更改堆栈面板的可见性?我有以下 Xaml

<StackPanel HorizontalAlignment="Right" 
            Orientation="Horizontal" 
            Grid.Column="1"
            Background="#FF7a7a7a">
    <StackPanel.Style>
        <Style TargetType="{x:Type StackPanel}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding SearchText}" Value="">
                    <Setter Property="Visibility" Value="Hidden"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </StackPanel.Style>
    Content....
   </StackPanel>

I Know that SearchTextgets updates and binds properly outside the StackPanel

我知道SearchText在外部获取更新并正确绑定StackPanel

Could somebody point me in the right direction?

有人能指出我正确的方向吗?

回答by Federico Berasategui

This:

这个:

<DataTrigger Binding="{Binding SearchText}" Value="">
   <Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>

will work for empty string (""), however it will not work for null.

将适用于空字符串 ( ""),但不适用于null.

Add another DataTriggerfor the null case:

DataTrigger为空情况添加另一个:

<DataTrigger Binding="{Binding SearchText}" Value="{x:Null}">
   <Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>

回答by Butsaty

Correct using String.Empty in XAML:

在 XAML 中正确使用 String.Empty:

xmlns:sys="clr-namespace:System;assembly=mscorlib"
...
<DataTrigger Binding="{Binding SearchText}" Value="{x:Static sys:String.Empty}">

回答by Tico

Weird as it might sound, the code below works for me:

听起来很奇怪,下面的代码对我有用:

<StackPanel Background="#FF7a7a7a">
    <StackPanel.Style>
        <Style TargetType="{x:Type StackPanel}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Text, ElementName=textBlock}" Value="">
                    <Setter Property="Visibility" Value="Hidden"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </StackPanel.Style>
    <TextBox x:Name="textBlock" Text="" Width="100" Height="30"></TextBox>
</StackPanel> 

Can you tell the value your Property is sending?

你能说出你的财产发送的价值吗?

回答by Rahul Misra

Try this

尝试这个

<StackPanel.Style>
                                        <Style TargetType="StackPanel">
                                            <Setter Property="Visibility" Value="Collapsed"/>
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding YourBoundPropertyName}" Value="True">
                                                    <Setter Property="Visibility" Value="Visible"/>

                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </StackPanel.Style>