WP7 相当于 EmptyDataTemplate?
许多 ASP.NET 数据绑定控件公开一个 EmptyDataTemplate,该模板在控件绑定到空数据源时呈现。在我的 WP7 应用程序中,我也想在绑定到 ListBox 的数据源为空时显示一条友好的消息。有没有一种相当优雅的方法来实现这一目标?最好与 calibburn.micro 集成/能够集成?
谢谢!!
A number of ASP.NET data bound controls expose an EmptyDataTemplate that is rendered when the control is bound to an empty data source. In my WP7 app, I too would like to display a friendly message when the data source that is bound to the ListBox is empty. Is there a reasonably elegant way to achieve this? Preferably integrated/able with caliburn.micro?
Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不喜欢使用隐藏代码来实现这样的功能。我宁愿建议实现一个可在绑定标记中使用的 DataTemplateConverter 来实现这个确切的功能。
例如:
转换器将在 xaml 文件的资源部分中实例化。
在这种情况下,Empty/NonEmpty 实现由您决定。
要了解如何实现此类 ValueConverter,请参阅 MSDN(或 google)
已添加示例。您可以使用 DataTemplate 的依赖属性,但为了简洁起见,我在这里省略了这一点。
编辑:
实现相同目标的其他方式,但“Silverlight 方式”多一点。使用 GoToStateAction 和适当的触发器。将模板图形封装在 UserControl 中,并指定此 UserControl 的状态。这样,用户控件将根据触发器的行为(空/非空)而改变。
结果将与我之前的提议相同,但具有状态更改动画的额外好处,这很难使用 DataTemplateConverter 实现(修改后的 TransitioningContentControl)。
I don't like using code behind for such a functionality. I would rather recommend implementing a DataTemplateConverter useable in the binding markup to achieve this exact functionality.
for exemple:
the converter would be instantiated in the resource section of the xaml file.
In this case, the Empty/NonEmpty implementation is up to you.
To understand how you can implement such a ValueConverter, see MSDN (or google)
Sample added. You could use dependency properties for the DataTemplate, but for brievty I ometted this here.
Edit:
Other way of achieving the same goal, but a little more in the "Silverlight way". Use a GoToStateAction and the adequate trigger. Encapsulate your template graphics in an UserControl, and specify States for this UserControl. This way, the user control will change according to the trigger's behavior (Empty/not empty).
The result will be the same as my former proposition, but with the added benefit of state changes animations, which would be difficult to achieve (modified TransitioningContentControl) with a DataTemplateConverter.
不确定 caliburn.micro,但例如,如果您要绑定到
ObservableCollection
(在我看来,绑定到任何东西的最佳集合),则有CollectionChanged< /code> 事件处理程序。
也就是说:
在这里,在事件处理程序本身中,您可以检查触发集合是否为空:
Not sure about caliburn.micro, but for example, if you are binding to an
ObservableCollection<T>
(in my opinion, the best collection to bind to anything), there is theCollectionChanged
event handler.So to say:
Here, in the event handler itself you can check whether the triggering collection is empty or not:
Silverlight 中不存在这样的现成功能。
但是,您可以做的是使用正确的消息创建一个
TextBlock
,并使用转换器将其可见性与ListBox
的 ItemsSource 绑定。当Count > 时,该转换器应返回Visibility.Visible
。当 Count == 0 时,为 0 和Visibility.Collapsed
。There is no such functionality out of the box in Silverlight.
What you can do however is create a
TextBlock
with a proper message and bind it's visibility with theListBox
's ItemsSource using a converter. That converter should returnVisibility.Visible
when Count > 0 andVisibility.Collapsed
when Count == 0.