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

时间:2011-09-12 07:50:52

标签: c# silverlight xaml windows-phone-7

我正在尝试使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);  

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

当它不应该可见时将高度/宽度设置为0