【问题标题】:Silverlight: Remove loading message on content load without code behind?Silverlight:在没有代码的情况下删除内容加载时的加载消息?
【发布时间】:2010-11-04 16:17:55
【问题描述】:

我正在 Silverlight 中构建一个 wp7 应用程序。我有一些异步加载的内容,以及指示加载尚未完成的消息。一旦内容的列表框不为空,我希望加载消息消失。是否可以仅在 XAML 中执行此操作?类似于将Visibility 属性绑定到StoryListBox.ItemsSource.IsEmpty

StoryListBox 在数据可用后通过将其ItemsSource 设置为可观察集合来填充。

    <TextBox x:Name="LoadingMessage"  Text="Loading..." Grid.Row="0" />
    <ProgressBar x:Name="LoadingProgress" IsIndeterminate="True" Style="{StaticResource PerformanceProgressBar}" />

    <ListBox x:Name="StoryListBox" Grid.Row="0" />

更新:我尝试了以下方法,但它不起作用:

  <StackPanel x:Name="Loading" Grid.Row="0" Visibility="{Binding StoryListBox.ItemsSource.IsEmpty, Converter={StaticResource visibilityConverter}}">
            <TextBox Text="Loading..." />
            <ProgressBar IsIndeterminate="True" Style="{StaticResource PerformanceProgressBar}" />
        </StackPanel>

        <ListBox x:Name="StoryListBox" Grid.Row="1" />

Loading 堆栈面板永远不会折叠。

【问题讨论】:

  • 如果您提供有关如何绑定 ContentListBox 的更多详细信息,我会有所帮助?它的ItemsSource 属性是在内容可用时分配的,还是只是绑定到添加项目的ObservableCollectionICollectionView? ContentListBox 是你的 xaml 中的“StoryListBox”吗,你能整理一下这种不一致吗??

标签: c# silverlight windows-phone-7


【解决方案1】:

您似乎已经回答了自己的问题。是的,您可以简单地将 Visibility(或 BusyIndi​​cator 控件上的 Busy/IsBusy 绑定到另一个控件的某个属性)。

如果您要绑定的特定属性不是可绑定属性,只需绑定到其他控件并自定义转换器以获得您想要的成员属性。如果您有具体的代码示例,只需发布​​它们,我可以发布更具体的解决方案。

通常的问题是类型(为了可见性)与布尔值不兼容,因此您需要在绑定中指定一个转换器。谷歌的 Silverlight VisibilityConvertor(一打一毛钱)。这是我的:

namespace Common.ValueConverters
{
    using System;
    using System.Windows;
    using System.Windows.Data;

    public class VisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is bool?)
            {
                if (string.IsNullOrEmpty((string)parameter))
                {
                    return (value as bool?).Value ? Visibility.Visible : Visibility.Collapsed;
                }
                else
                {
                    return (value as bool?).Value ? Visibility.Collapsed : Visibility.Visible;
                }
            }
            throw new ArgumentException();
        }

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

转换器的使用如下所示:

<Grid Visibility="{Binding ShowDualView, Converter={StaticResource VisibilityConverter}}">

但坦率地说,您最好使用绑定到 IsBusy 属性的 BusyIndi​​cator 控件:

<Controls:BusyIndicator IsBusy="{Binding IsBusy}">

只需将它放在您想被繁忙的显示器隐藏的控件周围。

【讨论】:

  • 谢谢。我不确定 XAML 会是什么样子。
  • @Rosarch:我添加了一些 Xaml 示例(用于转换器和 BusyIndi​​cator)。干杯
猜你喜欢
  • 2021-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多