0

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>
Ankit
  • 6,554
  • 6
  • 49
  • 71

1 Answers1

2

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}" 
    />
.......
Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137
  • hey, thanks. All these days I were uselessly creating NullValueToVisibilityConverter. :) – Ankit Mar 03 '14 at 07:31
  • you're welcome :) but looking at your code again, it looks more like windows phone (silverlight) instead of WPF. I've only tested this in WPF though – har07 Mar 03 '14 at 07:37
  • 1
    yes its windows phone, but it worked perfectly on it as well. – Ankit Mar 03 '14 at 07:53