在 WPF 中动态更改网格的可见性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22195871/
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 11:07:47  来源:igfitidea点击:

Dynamically change the Visibility of a Grid in WPF

c#wpfsilverlightwindows-phone-8visibility

提问by user3114009

I have a Gridwith TextBlockin it:

我有一个GridTextBlock它:

<Grid x:Name="GridLayout" Margin="4,0,4,1" Grid.Row="2" Background="#accdd7">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <TextBlock Name="Title" 
               Grid.Row="0"
               HorizontalAlignment="Stretch"
               Padding="10,2,10,2"
               Style="{StaticResource PromptTextStyle}" />
</Grid>

I am setting this TextBlockvalue programatically:

我正在以TextBlock编程方式设置此值:

Title.Text = myObject.Title;

Now here myObject.Titlemay be Null or Empty sometimes at that time I need to hide this entire Grid.

现在这里myObject.Title有时可能是 Null 或 Empty,那时我需要隐藏整个Grid.

How to acheive this?

如何实现这一目标?

回答by Rohit Vats

Set x:Nameon TextBlock. Then apply dataTriggerson Grid's style to collapsed the visibility when Textis set to null or empty string on TextBlock.

设置x:NameTextBlock。然后应用dataTriggersGrid 的样式Text在 TextBlock 上设置为 null 或空字符串时折叠可见性。

    <Grid xmlns:sys="clr-namespace:System;assembly=mscorlib"
          x:Name="GridLayout" Margin="4,0,4,1" Grid.Row="2" Background="#accdd7">
        <Grid.RowDefinitions>
           <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <TextBlock x:Name="Title" 
                   Grid.Row="0"
                   HorizontalAlignment="Stretch"
                   Padding="10,2,10,2"
                   Style="{StaticResource PromptTextStyle}"/>
        <Grid.Style>
            <Style TargetType="Grid">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Text, ElementName=Title}"
                                 Value="{x:Null}">
                        <Setter Property="Visibility" Value="Collapsed"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Text, ElementName=Title}" 
                                 Value="{x:Static sys:String.Empty}">
                        <Setter Property="Visibility" Value="Collapsed"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Grid.Style>
    </Grid>

回答by Jaihind

Try this, in code behind

试试这个,在后面的代码中

if(string.IsNullOrEmpty(myObject.Title))
{
GridLayout.Visibility = Visibility.Collapsed;
Title.Text=string.Empty;
}
else
{
Title.Text = myObject.Title;
GridLayout.Visibility = Visibility.Visible;
}

回答by user3114009

May the answer above is for Windows Phone 7

可能以上答案适用于 Windows Phone 7

I solved it myself.

我自己解决了。

Here is how i did.

这是我做的。

In the Xaml make the Visibility of the grid item to be collapsed by default, and now in code check the myObject.Title is null or not. if not null then set grid visibilty to visible.

在 Xaml 中默认折叠网格项的可见性,现在在代码中检查 myObject.Title 是否为空。如果不为空,则将网格可见性设置为可见。