繁体   English   中英

使用Visibilty.Collapsed(WP7)使TextBlock不可见的问题

[英]Issue in making TextBlock invisible using Visibilty.Collapsed (WP7)

我正在尝试使TextBlocks折叠(不可见),但是即使TextBlocks不可见,也仍然会获得空格/线条。 我在许多文章中都看到要使TextBlock完全不可见(没有行空间),使用了可见性属性Collapsed

请找到下面给出的代码,并让我知道我在做什么错。 我见过非常相似的帖子,但那些帖子没有回答这个问题。

Silverlight(WPF)代码:

 <phone:PhoneApplicationPage 
        x:Class="AndBI.MainGamePage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
        xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        FontFamily="{StaticResource PhoneFontFamilyNormal}"
        FontSize="{StaticResource PhoneFontSizeNormal}"
        Foreground="{StaticResource PhoneForegroundBrush}"
        SupportedOrientations="Portrait" Orientation="Portrait"
        mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
        shell:SystemTray.IsVisible="True">
    <Grid x:Name="LayoutRoot" Background="Transparent" >
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="All is well" Style="{StaticResource PhoneTextNormalStyle}"/>
        </StackPanel>

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,-14">
            <Button Content="Go" Height="72" HorizontalAlignment="Right" Margin="0,29,0,0" Name="GO" VerticalAlignment="Top" Width="99" Click="GO_Click" />
            <TextBox Height="72" HorizontalAlignment="Left" Margin="120,29,0,0" Name="textBox1"  VerticalAlignment="Top" Width="244" DataContext="{Binding}">
            </TextBox>
            <TextBlock Height="34" HorizontalAlignment="Left" Margin="16,50,0,0" Name="c10" Text="?" VerticalAlignment="Top" Width="112" TextWrapping="NoWrap" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="20,285,0,0" Name="b10" Text="aa" VerticalAlignment="Top" Width="112" Foreground="#FFFE6943" FontWeight="Bold" />
            <TextBlock Height="30" Margin="176,285,0,0" Name="user10" Text="bb" VerticalAlignment="Top" Foreground="#FFFE6943" HorizontalAlignment="Left" Width="111" FontWeight="Bold" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="322,285,0,0" Name="c11" Text="cc" VerticalAlignment="Top" Width="105" Foreground="#FFFE6943" FontWeight="Bold" />
            <Button Content="Scratch Pad" Height="72" HorizontalAlignment="Left" Margin="262,207,0,0" Name="SP" VerticalAlignment="Top" Width="194" Click="SP_Click" />
            <ListBox Height="57" Background="White" ItemsSource="{Binding}" HorizontalAlignment="Left" Margin="16,144,0,0" Name="listBox1" VerticalAlignment="Top" Width="434" >
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                    </Style>
                </ListBox.ItemContainerStyle>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid HorizontalAlignment="Stretch" Background="Red">
                            <Grid.RowDefinitions>
                                <RowDefinition  />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Text="Text1" Margin="0,0,10,0" HorizontalAlignment="Stretch" Grid.Column="0" Grid.Row="0" />
                            <TextBlock Text="Text2" Margin="0,0,10,0" HorizontalAlignment="Stretch" Grid.Column="1" Grid.Row="0"  />
                            <TextBlock Text="Text3" Margin="0,0,10,0" HorizontalAlignment="Stretch" Grid.Column="2" Grid.Row="0"  />
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>
        </Grid>
    </phone:PhoneApplicationPage> 

后面的C#代码:

private void CollapseAll(TextBlock tb, Boolean visible)
{
    tb.Visibility = (!visible) ? Visibility.Collapsed : Visibility.Visible;
}

/**Based upon a criteria the TextBlocks are made invisible as in collapsed**/ 

CollapseAll(c10,false);  
CollapseAll(b10,false);    
CollapseAll(user10,false);  
......................
......................  
/**Based upon a criteria the TextBlocks are made visible**/ 
CollapseAll(c10,true);  
CollapseAll(b10,true);    
CollapseAll(user10,true);  

当高度/宽度不可见时,将其设置为0

我可以想象所有的边距都会产生所有的边距。 我建议您使用不同的布局包含,并仅使用边距来增加元素周围的可用空间,以更好地使用而不是出于布局目的。

如果您希望项目仍然占据页面上的空间但不可见,而不是更改控件的可见性,请将其不透明度设置为零。
这使它可以保留在视觉树中并仍然占据树液。
如果将可见性设置为折叠,则将其从视觉树中删除。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM