Issue in making TextBlock invisible using Visibilty.Collapsed (WP7)
I am trying to make the TextBlocks
collapsed (invisible) but still get a space/line even though the TextBlocks
are invisible.
I have seen on many posts that to make a TextBlock
completely invisible (no line space), the visibility property - Collapsed
is used.
Please find the code given below, and let me know what I am doing wrong. I have seen very similiar posts but those do not answer this issue.
Silverlight(WPF) code :
<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" />
<Butto开发者_C百科n 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# code behind:
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);
I can imagine that all the margins result in all the margin. I would recommand that you use the different layout contains and use the margin only to increase the free space around the element for better usage and not for layout purposes.
If you want items to still take up space on the page but not be visible, rather than change the controls visibility, set it's opacity to zero instead.
This allows it to remain in the visualtree and still take up sapce.
If you set visibility to collapsed it is removed from the visual tree.
Set the height/width to 0 when it should not be visible
精彩评论