2015-08-26 59 views
0

我有一個列表視圖具有數據模板,其文本塊與文本值綁定到數據。如何重新打印xaml中的所有綁定更新後(windows phone 8.1)

<ListView ItemsSource="{Binding Items}" 
       Grid.Row="2" 
       HorizontalAlignment="Left" 
       Margin="10,70" 


       Foreground="Red" > 
     <ListView.ItemTemplate > 

      <DataTemplate> 

       <TextBlock x:Name="TextBlock_PlayerOneBid" 
            TextWrapping="Wrap" 
            Text="{Binding PlayerOneBid}" 
            FontSize="20" 
            FontFamily="ArialBlack" 
            Height="25" 
            Width="23" 
            TextAlignment="Center" 
            /> 

       <TextBlock x:Name="TextBlock_PlayerTwoBid" 
            TextWrapping="Wrap"         
            Text="{Binding PlayerTwoBid}" 
            FontSize="20" 
            FontFamily="ArialBlack" 
            Height="25" 
            Width="23" 
            TextAlignment="Center" 
            /> 

       </DataTemplate> 


     </ListView.ItemTemplate> 



    </ListView> 

im問題在這裏是這個數據模板打印兩次。有4個文本塊而不是2個。有沒有一種方法可以將模板從打印中分離出來,直到所有的值都被設置好?

設置值的代碼。

PlayerOneBid = 1 ; 
PlayerTwoBid = 2 ; 

回答

1

創建可視化轉換器,它會返回Collapsed當字符串爲空或空的,否則Visible

public sealed class EmptyToVisibilityConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, string language) 
    { 
    if (value == null) return Visibility.Collapsed; 
    var str = value.ToString(); 
    return String.IsNullOrEmpty(str) ? Visibility.Collapsed : Visibility.Visible; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, string language) 
    { 
    throw new NotImplementedException(); 
    } 
} 

然後在網頁上,宣告資源:

然後在每個TextBlock添加使用轉換器:

<ListView ItemsSource="{Binding Items}" 
       Grid.Row="2" 
       HorizontalAlignment="Left" 
       Margin="10,70" 


       Foreground="Red" > 
     <ListView.ItemTemplate > 

      <DataTemplate> 

       <TextBlock x:Name="TextBlock_PlayerOneBid" 
            TextWrapping="Wrap" 
            Text="{Binding PlayerOneBid}" 
            Visibility="{Binding PlayerOneBid, Converter={StaticResource EmptyToVisibilityConverter}}" 
            FontSize="20" 
            FontFamily="ArialBlack" 
            Height="25" 
            Width="23" 
            TextAlignment="Center" 
            /> 

       <TextBlock x:Name="TextBlock_PlayerTwoBid" 
            TextWrapping="Wrap"         
            Text="{Binding PlayerTwoBid}" 
            Visibility="{Binding PlayerTwoBid, Converter={StaticResource EmptyToVisibilityConverter}}" 
            FontSize="20" 
            FontFamily="ArialBlack" 
            Height="25" 
            Width="23" 
            TextAlignment="Center" 
            /> 

       </DataTemplate> 


     </ListView.ItemTemplate> 



</ListView>