LongListSelector为空样式

时间:2014-10-23 13:08:05

标签: xaml windows-phone-8 longlistselector

我想在LongListSelector没有项目时显示TextBlock。我可以在我的应用程序中的每个列表中执行此操作,但这不是很明智。所以我正在尝试编辑列表样式并对每个列表执行此操作。

如何在其样式中绑定LongListSelector ItemsSouce计数?

这是我现在的位置。

<Style TargetType="phone:LongListSelector">
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="phone:LongListSelector">
                <Grid Background="{TemplateBinding Background}">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="ScrollStates">
                            <VisualStateGroup.Transitions>
                                <VisualTransition GeneratedDuration="00:00:00.5" />
                            </VisualStateGroup.Transitions>
                            <VisualState x:Name="Scrolling">
                                <Storyboard>
                                    <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="VerticalScrollBar" />
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="NotScrolling" />
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Grid Margin="{TemplateBinding Padding}">
                        <i:Interaction.Triggers>
                            <!-- HERE I CANT BIND TO COUNT -->
                            <ec:DataTrigger Binding="{TemplateBinding ItemsSource}" Comparison="Equal" Value="0">
                                <ec:ChangePropertyAction TargetObject="{Binding ElementName=EmptyListText}" PropertyName="Visibility" Value="Visible" />
                            </ec:DataTrigger>
                        </i:Interaction.Triggers>
                        <ViewportControl x:Name="ViewportControl" HorizontalContentAlignment="Stretch" VerticalAlignment="Top" />
                        <ScrollBar x:Name="VerticalScrollBar" Opacity="0" Background="{StaticResource ThemeBackground}" HorizontalAlignment="Right" Orientation="Vertical" />
                        <TextBlock x:Name="EmptyListText" Visibility="Collapsed" Text="{Binding Source={StaticResource Literals}, Path=Literals.noResults}" />
                    </Grid>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

2 个答案:

答案 0 :(得分:0)

基于长列表选择器创建自定义复合控件,该列表选择器执行所需操作并具有智能来显示每个依赖项属性的特定消息。

答案 1 :(得分:0)

这是一个DataBinding问题。

尝试将它附加到TemplateParent(这是LongListSelector),如下所示:

<ec:DataTrigger
    Binding="{Binding RelativeSource={RelativeSource Mode=TemplateParent}, Path=ItemsSource.Count}"
    Comparison="Equal" Value="0">
</ec:DataTrigger>

如果你想使用你的触发器,这应该让你直截了当。


我喜欢使用Converters。理解起来有点简单,所以这里是使用转换器的完整解决方案。

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Data;

public class MyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null)
            return Visibility.Collapsed;
        else
        {
            if (value is System.Collections.IList)
            {
                System.Collections.IList list = (System.Collections.IList)value;
                if (list.Count == 0)
                    return Visibility.Collapsed;
                else
                    return Visibility.Visible;

            }
            else
                return Visibility.Collapsed;                
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
}

所以LongListSelector样式是:

<TextBlock Visibility="{Binding ItemsSource, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource MyConverter}}"></TextBlock>