2014-03-03 106 views
0

我已經將viewmodel設置爲頁面的datacontext,並將綁定透視項目與Individual集合屬性綁定。然而,綁定工作正常時,有數據,但最初當數據不可用時,它不能正常工作,我基本上想隱藏ItemsControl下的文本框,但不知道這個代碼有什麼問題。當綁定源爲空時,控件未被隱藏

<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> 

回答

2

DataContext時爲空,結合將使用TargetNullValue屬性指定值。因此,嘗試設置的Visibility結合CollapsedTargetNullValue財產和你won't even need converter

....... 
<Image 
    ....... 
    Visibility="{Binding userImage, TargetNullValue=Collapsed}" 
    /> 
....... 
+0

嘿嘿,謝謝。所有這些日子我都無用地創建了NullValueToVisibilityConverter。 :) – Ankit

+0

不客氣:)但再次查看您的代碼,它看起來更像是Windows Phone(Silverlight)而不是WPF。我只在WPF上測試過它,雖然 – har07

+1

是它的Windows手機,但它在它上面工作得很好。 – Ankit

相關問題