使用 Visibilty.Collapsed (WP7) 使 TextBlock 不可见时出现问题
我试图使 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);
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" />
<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# 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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我可以想象所有的利润都会导致所有的利润。我建议您使用不同的布局包含,并仅使用边距来增加元素周围的可用空间,以便更好地使用,而不是用于布局目的。
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.
当不应该可见时将高度/宽度设置为 0
Set the height/width to 0 when it should not be visible