如果bool = true,如何在LongListSelector中显示元素?

时间:2014-02-27 13:13:32

标签: windows-phone-8 boolean longlistselector

我有一个绑定到ObservableCollection(MyObject)的LongListSelector我希望只在myobject.BoolProperty = True时在容器(Grid)中显示图像。我怎样才能做到这一点 ? 不幸的是,似乎我们无法从后面的代码访问LongListSelector内的元素...在我的xaml页面中,我只想在布尔属性为真时显示图像(IsSuscribed)...

   
<phone:LongListSelector x:Name="llsAllDoclibs" 
                                    LayoutMode="List" 
                                    ItemsSource="{Binding}" 
                                    HideEmptyGroups="False"    
                                    Background="WhiteSmoke"
                                    SelectionChanged="Doclibs_SelectionChanged"
                                    Margin="20,20,20,20"
                                    >

                <phone:LongListSelector.ItemTemplate >
                    <DataTemplate >
                        <Grid   
                            Height="auto"
                            Margin="0,20,0,0" 
                            Background="White"
                            >

                            <StackPanel Margin="10,10,10,10">
                                <Image HorizontalAlignment="Right"
                                       VerticalAlignment="Center"
                                       Source="/Images/BDocs/ico_action_valider.png"
                                       x:Name="IsSuscribed"></Image>
                                <TextBlock
                                    Text="{Binding NomDocLib}"
                                    Foreground="#FF00485A"/>
                                <TextBlock
                                    HorizontalAlignment="Right"
                                    VerticalAlignment="Top"
                                    Text="{Binding NbrElements}"
                                    Foreground="#FF00485A"/>
                            </StackPanel>                            

                        </Grid>
                    </DataTemplate>
                </phone:LongListSelector.ItemTemplate>

            </phone:LongListSelector>

修改

我实现了这个代码。它运行正常。但我想知道如何使用这个类的int值。如果值> 10我想显示我的元素,否则我想隐藏它。我尝试用int替换对象值,但我有一个错误...

   
public sealed class BooleanToVisibilityConverter : IValueConverter
        {

        public object Convert(object value, Type targetType, object parameter, CultureInfo language)
        {
            return (value is bool && (bool)value) ? Visibility.Collapsed : Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo language)
        {
            return value is Visibility && (Visibility)value == Visibility.Visible;
        }

}

1 个答案:

答案 0 :(得分:0)

将以下属性添加到Grid

Visibility="{Binding BoolValue,Converter={StaticResource BooleanToVisibilityConverter}}"

编写这样的转换器(实际上是一行)应该相当容易,但它是许多框架的一部分。