在列表框itemtemplate中的Windows手机可见性数据绑定

时间:2012-01-26 23:02:43

标签: c# windows-phone-7 xaml

我在listbox itemtemplate中遇到控件可见性问题。以下是将数据绑定到imagetools的Visibility属性的代码:xaml中的AnimatedImage和Textblock:

<ListBox x:Name="listSellers" ItemsSource="{Binding TagList}" SelectionChanged="listSellers_SelectionChanged" ScrollViewer.VerticalScrollBarVisibility="Disabled">
                                    <ListBox.ItemTemplate>
                                        <DataTemplate>
                                            <Grid Margin="0,12,0,12" Height="132">
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="Auto" MinWidth="107"/>
                                                    <ColumnDefinition/>
                                                </Grid.ColumnDefinitions>
                                                <Border Grid.Column="0" Margin="0,0,-2,8">
                                                    <Grid>
                                                        <imagetools:AnimatedImage Source="{Binding Seller.Logo, Converter={StaticResource ImageConverter}}" Stretch="Uniform" Width="240" Template="{StaticResource AnimatedImageControlTemplate1}" Visibility="{Binding LogoVisibility}"/>
                                                        <TextBlock x:Name="sellerNameTxtBlock" TextWrapping="Wrap" Text="{Binding Seller.Name}" FontSize="24" FontFamily="Segoe WP" Margin="10,0,0,0" Foreground="#FF354F59" Height="41" Visibility="{Binding Path=SellerNameVisibility}"/>
                                                    </Grid>
                                                </Border>
                                                <StackPanel Grid.Column="1" Orientation="Vertical">
                                                    <!--<TextBlock TextWrapping="Wrap" Text="{Binding Seller.Name}" FontSize="24" FontFamily="Segoe WP" Margin="10,0,0,0" Foreground="#FF354F59" Height="41"/>-->
                                                    <!--<TextBlock TextWrapping="Wrap" Text="amazon.com" FontSize="16" FontFamily="Segoe WP" Margin="10,0,0,0" Foreground="#FF157CCC" Height="35"/>-->
                                                    <TextBlock TextWrapping="Wrap" Text="{Binding TotalPrice}" FontSize="21.333" FontFamily="Segoe WP Semibold" Margin="10,0,0,0" Foreground="#cc4225"/>
                                                    <TextBlock FontSize="{StaticResource PhoneFontSizeSmall}" Text="{Binding Price}" Margin="10,0,0,0" Foreground="#FF354F59"/>
                                                    <TextBlock FontSize="{StaticResource PhoneFontSizeSmall}" Text="{Binding Tax}" Margin="10,0,0,0" Foreground="#FF354F59"/>
                                                    <TextBlock FontSize="{StaticResource PhoneFontSizeSmall}" Text="{Binding Shipping}" Margin="10,0,0,0" Foreground="#FF354F59"/>
                                                </StackPanel>
                                            </Grid>
                                        </DataTemplate>
                                    </ListBox.ItemTemplate>
</ListBox>

以下是视图模型中属性的声明:

public Visibility LogoVisibility
        {
            get { return (Visibility)GetValue(LogoVisibilityProperty); }
            set { SetValue(LogoVisibilityProperty, value); }
        }

        // Using a DependencyProperty as the backing store for LogoVisibility.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty LogoVisibilityProperty =
            DependencyProperty.Register("LogoVisibility", typeof(Visibility), typeof(ProductDetailViewModel), new PropertyMetadata(Visibility.Collapsed));

        public Visibility  SellerNameVisibility
        {
            get { return (Visibility )GetValue(SellerNameVisibilityProperty); }
            set { SetValue(SellerNameVisibilityProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SellerNameVisibility.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SellerNameVisibilityProperty =
            DependencyProperty.Register("SellerNameVisibility", typeof(Visibility), typeof(ProductDetailViewModel), new PropertyMetadata(Visibility.Collapsed));

以下是我在视图模型中设置可见性的地方:

foreach (Tag tag in tagArray)
                {
                    if (tag.Seller.Logo.Equals(""))
                    {
                        tag.Seller.Logo = "Images/NoImageFound.png";
                        LogoVisibility = Visibility.Collapsed;
                        SellerNameVisibility = Visibility.Visible;
                    }
                    else
                    {
                        LogoVisibility = Visibility.Visible;
                        SellerNameVisibility = Visibility.Collapsed;
                    }
                    tag.Price = "Base: " + tag.Price;
                    if (tag.Tax == null)
                    {
                        tag.Tax = "Tax: N/A";
                    }
                    else
                    {
                        tag.Tax = "Tax: " + tag.Tax;
                    }
                    if (tag.Shipping == null)
                    {
                        tag.Shipping = "Ship: N/A";
                    }
                    else
                    {
                        tag.Shipping = "Ship: " + tag.Shipping;
                    }
                    tempTagList.Add(tag);
                }
                TagList = tempTagList;
            }

这是我在后面的代码中设置datacontext的地方:

protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            _productDetailViewModel = new ProductDetailViewModel();
            DataContext = _productDetailViewModel;
            string productTitleId = "";
            if (NavigationContext.QueryString.TryGetValue("productTitleId", out productTitleId))
            {
                _productTitleId = productTitleId;
                _productDetailViewModel.getProductDetailFromServer(_productTitleId, "");
            }
        }

我还在这些文件中做了一些其他数据绑定,所有这些都有效。只有此可见性绑定失败。真的不知道为什么。 =(任何想法?谢谢!!!

1 个答案:

答案 0 :(得分:2)

看起来您的ListBox ItemsSource绑定到“Tag”对象的集合。这意味着每个ListBoxItem都将绑定到“Tag”对象。因此,您正在创建的DataTemplate具有等于一个Tag对象的DataContext。我没有看到Logo对象上有LogoVisibility。它看起来像是在对象上,它包含对标记列表的引用。走这条路线,你会想要Tag对象本身的LogoVisibility和NameVisibility。

我建议不要将该逻辑放入模型对象(Tag)中,而是使用ValueConverter来处理此逻辑。

    public class EmptyToVisibilityConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null) return Visibility.Collapsed;

        string val = value.ToString();

        return string.IsNullOrWhiteSpace(val) ? Visibility.Collapsed : Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

然后将您的可见性绑定更改为:

    <Grid DataContext="{Binding Seller}">
        <imagetools:AnimatedImage Source="{Binding Logo, Converter={StaticResource ImageConverter}}" 
                              Stretch="Uniform" Width="240" 
                              Template="{StaticResource AnimatedImageControlTemplate1}" 
                              Visibility="{Binding Logo, Converter={StaticResource EmptyToVis}}"/>
        <TextBlock x:Name="sellerNameTxtBlock" TextWrapping="Wrap" Text="{Binding Name}" FontSize="24" 
               FontFamily="Segoe WP" Margin="10,0,0,0" Foreground="#FF354F59" Height="41" 
               Visibility="{Binding Name, Converter={StaticResource EmptyToVis}}"/>
    </Grid>