简体   繁体   English

尝试在WPF应用程序中将网格线设置为屏幕上的动画,并检测一次可以在屏幕上显示的数量

[英]Trying to animate lines of a grid onto the screen in a WPF app and detect how many can fit on the screen at a time

I am creating a food menu that displays on a TV screen in a restaurant. 我正在创建一个食物菜单,显示在餐厅的电视屏幕上。 There will be no interaction of the menu itmes, so I need to automatically display the items and rotate through them when all the items do not fit on the screen at one time. 菜单主题没有交互,所以我需要自动显示项目,并在所有项目一次不适合屏幕时旋转它们。

I have all of the components of the project built, even the administration of what the menu items are and which day to display them. 我已经构建了项目的所有组件,甚至管理了菜单项以及显示它们的日期。 Now, I need to finish the display of the items for the TV screen(s). 现在,我需要完成电视屏幕项目的显示。

I am new to WPF, so I'm trying to figure out the best way to do this: 我是WPF的新手,所以我想找出最好的方法:

  1. Menu items are listed with the name of the item along with calories, protein, fat, and cholesterol. 菜单项列出了项目名称以及卡路里,蛋白质,脂肪和胆固醇。 Because of this, they need to be displayed in a grid. 因此,它们需要以网格显示。
  2. There is no user interaction with the menu, so I need to make sure that I can properly break up the list of items into "pages" that animate on and off the screen after displaying for several seconds. 没有用户与菜单的交互,所以我需要确保我可以将项目列表正确地分解为“页面”,这些页面在显示几秒后显示屏幕上和屏幕外的动画。 The way I was thinking of doing this was to add each item on the view and evaluate how much space is available beneath it. 我想这样做的方法是在视图上添加每个项目并评估它下面有多少可用空间。 If the space is less than a certain height, the rest of the menu items are queued for display on the next "page". 如果空间小于某个高度,则其余菜单项排队等待显示在下一个“页面”上。 It would be nice to have each line item rapidly slide in from the left, having all of them appear on the screen within 1-2 seconds. 让每个订单项从左侧快速滑入,让所有订单项在1-2秒内显示在屏幕上会很不错。
  3. What is the best control to display them with? 显示它们的最佳控件是什么? An ItemsControl, perhaps? 也许是ItemsControl?

Any great ideas you guys have that can point me in the right direction will be greatly appreciated! 你们有任何伟大的想法可以指出我正确的方向将非常感谢!

Update: Here's a very rough mockup of what I'm talking about: 更新:这是我正在谈论的一个非常粗略的模型: 在此输入图像描述

You can probably get away with using a Grid or StackPanel and animate the margin value of your elements (use negative margins to slide items in). 您可以放弃使用Grid或StackPanel并为元素的边距值设置动画(使用负边距来滑动项目)。 You need to ensure that the parent element has ClipToBounds set to false so that items can be drawn outside of its bounds. 您需要确保父元素将ClipToBounds设置为false,以便可以在其边界之外绘制项。

To measure an element's height, just use the Measure method on the row element to figure out how much space it needs vertically. 要测量元素的高度,只需使用行元素上的Measure方法来确定它需要垂直的空间大小。 Based on that, you can determine how many elements you can fit in one page. 基于此,您可以确定一页中可以容纳的元素数量。

There's too much work to build everything, however the following should lead you to your goal : 构建所有内容的工作量太大,但以下内容应该会引导您实现目标:

Inherit from Listview add create a 'PartialListView' , with added dependencies properties StartIndex and ItemHeight. 从Listview继承添加创建'PartialListView',添加依赖项属性StartIndex和ItemHeight。 It should build a listview of the right height showing items from StartIndex to StartIndex+ItemHeight. 它应该构建一个正确高度的列表视图,显示从StartIndex到StartIndex + ItemHeight的项目。 (scroller not shown. Use FindDescendant (of type scrollviewer) to find the scrollViewer after listView is loaded, and set its VerticalOffset) (滚动条未显示。使用FindDescendant(类型为scrollviewer)在listView加载后查找scrollViewer,并设置其VerticalOffset)

Create a UserControl with two dependency properties : 使用两个依赖项属性创建UserControl:
1) bind to an ObservableCollection of MenuItems (Item, Calories, ...) 1)绑定到MenuItems的ObservableCollection(Item,Calories,...)
2) an ItemHeight (integer property). 2)ItemHeight(整数属性)。

Then on each collection change you modify IndexList, an Observable collection of numbers {1, 6, 11 }, which are the first indexes of each menu. 然后在每个集合更改时修改IndexList,一个Observable数字集合{1,6,11},它们是每个菜单的第一个索引。

The control code will look like : 控制代码如下所示:

            <ListBox ItemsSource="{Binding IndexList, ElementName = mycontrolname}">
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <ScrollViewer x:Name="PageScroller"  ... set to horizontal only, not
                                 showing any bar > 
                        <StackPanel Orientation="Horizontal" />
                        </ScrollViewer> 
                   </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                    <ListBox.ItemTemplate>
                <DataTemplate>
                   <app:PartialListView   StartIndex="{Binding }"  
                                 IndexCount  = "{Binding IndexCount, ElementName = mycontrolname}"
                                 ItemsSource = "{Binding MenuItems , ElementName = mycontrolname}"
                   />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

switching 'screen' will be done by changing the HorizontalOffset of the PageScroller ScrollViewer. 切换'屏幕'将通过更改PageScroller ScrollViewer的Horizo​​ntalOffset来完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM