问题描述
我已将视图模型设置为页面的数据上下文,并将数据透视项与个人集合属性绑定.然而,当有数据时绑定工作正常,但最初当数据不可用时它无法正常工作,我基本上想隐藏 ItemsControl 下方的文本框,但不知道这段代码有什么问题.
I have set viewmodel as page's datacontext, and binding pivot items with an Individual collection property. However the binding is working correctly when there is data, but initially when data is not available it is not working properly, I basically want to hide the textbox below the ItemsControl but don't know whats wrong with this code.
<phone:PivotItem Header="Tweets" Margin="{StaticResource PivotItemMargin}" DataContext="{Binding Tweets}">
<ScrollViewer>
<StackPanel>
<ItemsControl ItemsSource="{Binding Result}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Margin="0,0,0,30">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="35" />
<RowDefinition Height="75" />
</Grid.RowDefinitions>
<Image Grid.Column="0" Grid.RowSpan="2" Source="{Binding userImage}" Margin="0,0,10,0" MaxWidth="100" MaxHeight="100" Stretch="UniformToFill" Visibility="{Binding userImage, Converter={StaticResource NullToVisibilityConverter}}" />
<TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding name}" Foreground="{StaticResource AppForegroundHighlightTextBrush}" TextWrapping="NoWrap" TextTrimming="WordEllipsis" FontSize="{StaticResource TitleFontSize}"/>
<TextBlock Grid.Column="1" Grid.Row="1" Text="{Binding ShortTitle}" Foreground="{StaticResource AppForegroundSubtextBrush}" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="{StaticResource SubtextFontSize}"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<TextBlock DataContext="{Binding Tweet}" Grid.Row="2" Text="all Tweets" FontWeight="Bold" Visibility="{Binding hasMore,Converter={StaticResource BooleanToVisibilityConverter}}"/>
</StackPanel>
</ScrollViewer>
</phone:PivotItem>
推荐答案
当 DataContext
为 null 时,绑定将使用 TargetNullValue
属性中指定的值.因此,尝试将 Visibility
绑定的 TargetNullValue
属性设置为 Collapsed
并且您甚至不需要转换器 :
When DataContext
is null, binding will use value specified in TargetNullValue
property. Therefore, try to set TargetNullValue
property of Visibility
binding to Collapsed
and you won't even need converter :
.......
<Image
.......
Visibility="{Binding userImage, TargetNullValue=Collapsed}"
/>
.......
这篇关于绑定源为空时控件不被隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!