空列表的列表框项目模板

发布于 2024-10-23 11:41:01 字数 1337 浏览 2 评论 0 原文

我有一个列表框,其中包含在 XAML 中定义的项目模板,如下所示:

        <ListBox Name="listBoxDisruptions">

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Margin="0,0,0,10">
                        <TextBlock Text="{Binding text}" Foreground="Black" FontSize="29">Hello! some item</TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>             

        </ListBox>

现在我想要的是在列表框的中心显示一行文本,以防该列表框的 ItemSource 为空。

XAML 是否支持某种无项目模板?像这样的东西:

    <ListBox Name="listBoxDisruptions">

                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Margin="0,0,0,10">
                            <TextBlock Text="{Binding text}" Foreground="Black" FontSize="29">Hello! some item</TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate> 

<ListBox.NoItemTemplate>
<TextBlock Text="No Items to display"/>
</ListBox.NoItemTemplate>                   

            </ListBox>

所以?

I have a list box with an item template defined in XAML like this:

        <ListBox Name="listBoxDisruptions">

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Margin="0,0,0,10">
                        <TextBlock Text="{Binding text}" Foreground="Black" FontSize="29">Hello! some item</TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>             

        </ListBox>

Now what i want is to display a line of text in the center of the listbox in case the ItemSource for this listbox is empty.

Does XAML support some kind of no item template, ? something like this:

    <ListBox Name="listBoxDisruptions">

                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Margin="0,0,0,10">
                            <TextBlock Text="{Binding text}" Foreground="Black" FontSize="29">Hello! some item</TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate> 

<ListBox.NoItemTemplate>
<TextBlock Text="No Items to display"/>
</ListBox.NoItemTemplate>                   

            </ListBox>

So ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寒江雪… 2024-10-30 11:41:01

可能有一种 XAML 方法可以使用类似 WPF 的技术来完成此操作 - 列表框项模板一个空列表

然而,在 Overflow7 中,我厌倦了尝试让这些工作 - 所以我使用了一个稍微有点 hacky 的技巧,而不是向页面添加一个额外的 TextBlock,然后使用:(

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) { 
            listBox1.ItemsSource = data; 

            data.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(data_CollectionChanged); 

        } 

        void data_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { 
            if (data.Count == 0) 
                textBlock1.Visibility = Visibility.Visible; 
            else 
                textBlock1.Visibility = Visibility.Collapsed; 
        } 

http://forums.create.msdn.com/forums/p/70755/431687。 .aspx)

There might be a XAML way to do it using WPF-like techniques - Listbox Item Template for an empty list

However, in Overflow7 I got bored trying to make these work - so I used a slightly-hacky trick instead of adding an extra TextBlock to the page and then using:

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) { 
            listBox1.ItemsSource = data; 

            data.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(data_CollectionChanged); 

        } 

        void data_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { 
            if (data.Count == 0) 
                textBlock1.Visibility = Visibility.Visible; 
            else 
                textBlock1.Visibility = Visibility.Collapsed; 
        } 

(Trick learnt from http://forums.create.msdn.com/forums/p/70755/431687.aspx)

送舟行 2024-10-30 11:41:01

您可以将文本块放在列表视图之外,然后使用转换器将文本块的可见性绑定到您用于列表视图的列表的计数。

编辑:要求的示例:-

<ListView ItemsSource="{Binding MyItemSource}">
   <ListView.ItemTemplate>
     <DataTemplate>
       <Grid/>
     </DataTemplate>
   </ListView.ItemTemplate>
</ListView>
<TextBlock Text="Some text" Visibility="{Binding MyItemSource.Count, Converter={StaticResource CountToVisibilityConverter}}"/>

在页面资源中或应用程序中的资源字典中声明转换器,如下所示:

<converters:CountToVisibilityConverter x:Key="CountToVisibilityConverter" />

然后转换器可以是:

public sealed class CountToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string culture)
    {
        if (value != null)
        {
            var i = (Int32)value;

            if (i > 0)
                return Visibility.Collapsed;
            else
                return Visibility.Visible;
        }
        return Visibility.Collapsed;
    }

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

You could take the textblock outside of the listview, and then bind the visibility of the textblock to the count of the list your using for the listview, using a converter.

EDIT: Example as asked for:-

<ListView ItemsSource="{Binding MyItemSource}">
   <ListView.ItemTemplate>
     <DataTemplate>
       <Grid/>
     </DataTemplate>
   </ListView.ItemTemplate>
</ListView>
<TextBlock Text="Some text" Visibility="{Binding MyItemSource.Count, Converter={StaticResource CountToVisibilityConverter}}"/>

Declare the converter either in your page resources, or a resource dictionary in your app like so:

<converters:CountToVisibilityConverter x:Key="CountToVisibilityConverter" />

and then the converter could be:

public sealed class CountToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string culture)
    {
        if (value != null)
        {
            var i = (Int32)value;

            if (i > 0)
                return Visibility.Collapsed;
            else
                return Visibility.Visible;
        }
        return Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string culture)
    {
        return new NotImplementedException();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文