如何在加载时显示 "loading" gif 而不是 wpf 控制器?
How to display "loading" gif instead of wpf controler when loading?
考虑 wpf 中的 TextBlock
异步绑定到视图模型中的 属性,其中 get
使用耗时的方法。使用 xaml 代码中的 Fallback
标签,我可以将 TextBlock
的 Text
标签设置为 "Loading..."。
但我实际上有一个 ListBox
绑定到 IEnumerable<MyType>
,其中列表框项目显示 MyType
的不同字段。如何在加载有界 IEnumerable<MyType>
时显示 laoding gif(或任何类型的不同 wpf 元素)?
我想我可以将 Loading 元素的 Visibility
绑定到某种描述异步 属性 状态的 bool
,但我没有找到xaml 中的这样一个布尔值。如果 xaml 中不存在,我可以算出加载方法的状态并在视图模型中创建此 bool
。那会是实现它的最佳方式吗?
根据 ItemsSource
的状态,您可以更改 ControlTemplate
:
<ListBox ItemsSource="{Binding Items}">
<ListBox.Style>
<Style TargetType="ListBox">
<Style.Triggers>
<Trigger Property="ItemsSource" Value="{x:Null}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<Image Source="LoadingImage.png"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.Style>
</ListBox>
您应该阅读@Stephen Cleary 关于异步 MVVM 应用程序模式的文章:https://msdn.microsoft.com/en-us/magazine/dn605875.aspx。
您可以绑定到 NotifyTaskCompletion<IEnumerable<MyType>>
源 属性 并使用 DataTrigger
或简单绑定到 IsNotCompleted
属性 以显示 Image
直到 Result
属性 已设置:
<!-- Busy indicator -->
<Image Source="pic.png" Visibility="{Binding YourItemsSourceProperty.IsNotCompleted,
Converter={StaticResource BooleanToVisibilityConverter}}"/>
<!-- Results -->
<ItemsControl ItemsSource="{Binding YourItemsSourceProperty.Result}" Visibility="{Binding
UrlByteCount.IsSuccessfullyCompleted, Converter={StaticResource BooleanToVisibilityConverter}}"/>
请阅读这篇文章,了解有关该做和不该做的更多信息。
考虑 wpf 中的 TextBlock
异步绑定到视图模型中的 属性,其中 get
使用耗时的方法。使用 xaml 代码中的 Fallback
标签,我可以将 TextBlock
的 Text
标签设置为 "Loading..."。
但我实际上有一个 ListBox
绑定到 IEnumerable<MyType>
,其中列表框项目显示 MyType
的不同字段。如何在加载有界 IEnumerable<MyType>
时显示 laoding gif(或任何类型的不同 wpf 元素)?
我想我可以将 Loading 元素的 Visibility
绑定到某种描述异步 属性 状态的 bool
,但我没有找到xaml 中的这样一个布尔值。如果 xaml 中不存在,我可以算出加载方法的状态并在视图模型中创建此 bool
。那会是实现它的最佳方式吗?
根据 ItemsSource
的状态,您可以更改 ControlTemplate
:
<ListBox ItemsSource="{Binding Items}">
<ListBox.Style>
<Style TargetType="ListBox">
<Style.Triggers>
<Trigger Property="ItemsSource" Value="{x:Null}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<Image Source="LoadingImage.png"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.Style>
</ListBox>
您应该阅读@Stephen Cleary 关于异步 MVVM 应用程序模式的文章:https://msdn.microsoft.com/en-us/magazine/dn605875.aspx。
您可以绑定到 NotifyTaskCompletion<IEnumerable<MyType>>
源 属性 并使用 DataTrigger
或简单绑定到 IsNotCompleted
属性 以显示 Image
直到 Result
属性 已设置:
<!-- Busy indicator -->
<Image Source="pic.png" Visibility="{Binding YourItemsSourceProperty.IsNotCompleted,
Converter={StaticResource BooleanToVisibilityConverter}}"/>
<!-- Results -->
<ItemsControl ItemsSource="{Binding YourItemsSourceProperty.Result}" Visibility="{Binding
UrlByteCount.IsSuccessfullyCompleted, Converter={StaticResource BooleanToVisibilityConverter}}"/>
请阅读这篇文章,了解有关该做和不该做的更多信息。