滑块和标签/文本块控件交互 - WPF
我有一个滑块和一个标签控件。文本显示在标签中(几段)。
- 我一次只需要显示 3 个单词。每隔 1 秒,移动到下一组 3 个单词。
- 滑块用于选择一次可以看到的单词数。因此,用户可以将其增加到 10,现在每 1 秒需要显示一组 10 个单词。
我如何在 WPF 中实现这种行为?我知道我需要在滑块和标签之间进行某种数据绑定,但不确定如何获得(1)或(2)的效果。
任何帮助表示赞赏!
I have a slider and a label control. The text is displayed in the label (few paragraphs).
- I need to show only 3 words at a time.Every 1 second, move to the next set of 3 words.
- The slider is used to select the number of words that can be seen at once. So a user can increase it to say 10 and now every 1 second, a set of 10 words need to be displayed.
How would I achieve this behavior in WPF? I know I need to do some kind of databinding between the slider and a label, but not sure how to get the effect of (1) or (2).
Any help is appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是我在不使用 {edf:ExpressionBinding} 功能(可惜尚未公开提供)的情况下解决该问题的方法:
步骤 1:在类中创建三个 DependencyProperties(不是传统的 NET 属性):
步骤 2:绑定滑块到“WordsPerGroup”属性:
步骤 3:使用 LinearInt32KeyFrame 创建一个动画,为“GroupToShow”属性设置动画,该属性每秒计数一次并持续多久,例如持续 1 小时并计数到 3600:
步骤 4:创建一个接受“Text”、“GroupToShow”和“WordsPerGroup”并返回要显示的文本的转换器:
步骤 5:使用 MultiBinding 使用转换器绑定 TextBlock 的 Text 属性:
步骤 6:确保在加载时启动动画,或者当您希望动画开始移动时。
第 7 步:(可选)将 PropertyChangedCallback 添加到“GroupToShow”以检测单词何时全部显示并执行适当的操作(例如重新开始或停止动画)。
Here is how I would solve it without using my {edf:ExpressionBinding} feature (which, alas, is not yet publically available):
Step 1: Create three DependencyProperties (not traditional NET properties) in your class:
Step 2: Bind the Slider to the "WordsPerGroup" property:
Step 3: Create an animation using a LinearInt32KeyFrame to animate the "GroupToShow" property that counts once per second and lasts as long as you like, for example this lasts 1 hour and counts to 3600:
Step 4: Create a Converter that takes "Text", "GroupToShow" and "WordsPerGroup" and returns the text to display:
Step 5: Use a MultiBinding to bind the TextBlock's Text property using your converter:
Step 6: Make sure you start your animation on load, or whenever you want the animation to start moving.
Step 7: (optional) Add a PropertyChangedCallback to "GroupToShow" to detect when the words have all been shown and do something appropriate (like start over, or stop the animation).